PequeJS
A small Javascript library for manipulate DOM and send AJAX requests
Parameters
Examples
$(document)
$('input#username')
$('a', this)
Returns object PequeJS object.
Meta
- author: Alfonso Saavedra “Son Link”
- license: GPL-3.0-or-later
Constructor
Create the constructor
Parameters
selector
string The selector to use.parent
string? The father of the element. This is useful, for example, if you are looking for a class that is inside an element, and not outside it. (optional, defaultnull
)
html
Return the HTML of the firs element or set the HTML for one or more elements.
Parameters
html
string? If set change the HTML of the elements. If not return the HTML of the first element.
Examples
$('p').html();
$('p#title').html('<strong>PequeJS</strong>');
Returns string the HTML of the firts element found if html variable is not set.
text
Return the text of the firs element or set the text for one or more elements.
Parameters
txt
string? If set change the text of the elements. If not return the text of the first element found.
Examples
console.log($('span').text());
$('span#username').text('Son Link');
Returns string the HTML of the found element if html variable is not set.
on
Add event to the elements.
Parameters
event
string event (click, change, keyup, etc).selector
(string | function) If it is a string, add the event to the element(s) inside the parent, especially if the content is automatically generated. If passed a function it does the same as the callback parameter.callback
function? The callback function. (optional, defaultnull
)
Examples
$('button').on('click', function() {
alert('You clicked');
});
Returns undefined Returns undefined if event and callback parameters are empty.
show
Display the element(s).
Parameters
display
string? Set the display [block, flex, inline-block, etc] (optional, default'block'
)
Examples
$('#modal').show();
$('.blocks').show('flex');
hide
Hide the element(s).
Examples
$('#modal').hide();
attr
Set a element attribute or return the attribute of the firts element found.
Parameters
name
string The attribute namevalue
(string | number)? The value for the attribute. (optional, defaultnull
)
Examples
$('#mylink').attr('href', 'https://google.es');
link = $('#mylink').attr('href');
Returns string The attribute value of the first element found if value is not set.
removeAttr
Remove a element(s) attribute.
Parameters
name
string The attribute name
Examples
$('input#name').removeAttr('disabled');
append
Append a text or HTML to element(s).
Parameters
ele
string The text or HTML to append
Examples
$('ul#series').append('<li>The Rookie</li>');
empty
Clear all content of the element(s).
Examples
$('#series').empty();
hasClass
Return if any element have the class name.
Parameters
className
string The class name.
Examples
$('#modal').hasClass('show');
Returns boolean true if any element has the class or false if not.
addClass
Add class(es) to each element(s). You can add more than one separated by spaces.
Parameters
className
string The class(es) name.
Examples
$('#modal').addClass('show');
$('span#username').addClass('bold color-red');
removeClass
Remove class(es) on each element. You can remove more than one separated by spaces.
Parameters
className
string The class(es) name
Examples
$('#modal').removeClass('show');
$('span#username').removeClass('bold color-red');
toggleClass
Toggle class on each element.
Parameters
className
string The class name to toggle.
Examples
$('#modal').toggleClass('show');
val
Get or set the value for input, textarea or select.
Parameters
value
(string | number)? If set change the value of the firts find element. If not return her value.
Examples
$('#modal').toggleClass('show');
is
Checks if the current set of elements match a selector and returns true if at least one of these elements matches the given arguments.
Parameters
selector
String The CSS selector
Examples
$('input').is(':valid')
$('#accept-terms').is(':checked')
Returns any Bool
trigger
Trigger a event for the element.
Parameters
eventname
event
eventname The event to trigger
Examples
$('#myform').trigger('submit')
each
Iterate through every element of the collection. The callback function receive the current element.
Parameters
callback
Function The callback function
Examples
$('ul#series').each(function(ele) {
series.push($(ele).text());
});
Returns object The actual element
ajax
Perform an Ajax request.
Parameters
url
string The URL to send the request.opts
array A array to set the options. See below.opts.success
Fuction A callback function when the request was successful.opts.async
boolean [true] Set to false to block execution until you finish receiving the response.opts.contentType
object [application/x-www-form-urlencoded; charset=UTF-8] The content type for the request.opts.data
object [] A object with the variables and her values to send.opts.dataType
string [json] What kind of data do you expect to receive (json, text, html).opts.error
Function [] The callback when the request fail.opts.type
string [GET] The HTTP request method (GET, POST, etc).
Examples
$().ajax('http://localhost/api', {
data: {
user: 'myuser',
passwd: '123456'
},
type: 'POST',
success: function(response) {
console.dir(response);
}
});
get
Perform an GET request. This is a alias for $().ajax.
Parameters
url
string The URL to send the request.data
(object | Function) A object with the variables and her values to send. If is a function take this as the callback.callback
function [] The callback function when the request success or error.
Examples
$().get('http://localhost/api?key=123456789',
function(response) {
console.dir(response);
}
);
$().get('http://localhost/api', {key: '123456789'},
function(response) {
console.dir(response);
}
);
post
Perform an POST request. This is a alias for $().ajax.
Parameters
url
string The URL to send the request.data
object A object with the variables and her values to send. If is a function take this as the callback.callback
function [] The callback function when the request success or error.
Examples
$().post('http://localhost/api', {key: '123456789'},
function(response) {
console.dir(response);
}
);
instance
Create a new instance of the constructor
Parameters
selector
string The selector to use.parent
any? (optional, defaultnull
)
Returns object The instance of the library.
instance
Return the constructor instantiation