General Attributes Flashcards
Set attribute for a single element
element.attr(‘foo’, ‘bar’);
ment.setAttribute(‘foo’, ‘bar’);
Set attribute for a set of elements
elements.attr(‘foo’, ‘bar’);
ES6
elements.forEach(x => x.setAttribute(‘foo’, ‘bar’));
ES5
elements.forEach(function(element) { element.setAttribute(‘foo’, ‘bar’);
});
var value = element.getAttribute(‘foo’); Get plain object containing all element attributes as a name: value pairs
var value = element.attr(‘foo’);
var value = element.getAttribute(‘foo’);
Get a plain object containing all element attributes as key:value pairs
var attributes = element.attr();
ES6
var attributes = {};
for (var {name, value} of […element.attributes]) attributes[name] = value;
ES5
var attributes = {}; [].forEach.call(element.attributes, function(attr) {
attributes[attr.name] = attr.value;
});
Get the current value of the first element in the set of matched elements or set the value of every matched element. Get element value
var value = element.val();
var value = element.value;
element.val(‘foo’);
element.value = ‘foo’;
Set value of a set of elements
elements.val(‘foo’);
ES6
elements.forEach(x => x.value = ‘foo’);
ES5
elements. forEach(function(element) {
element. value = ‘foo’;
});