Menu Close

Can you pass this to an attribute in jQuery?

Can you pass this to an attribute in jQuery?

You cannot pass “this” to any method attribute on the element and use that argument to access the data. It will produce an undefined. The “data-” attribute prefix should be stripped off in your .data () call, so assuming: $ (” [attr1]”) or $ (” [attr1=’this is attr 1′]”) , which will return the element (not the attribute value)

How to get data attribute value from elements jQuery?

jQuery attr() Method. The jQuery attr() method is used to get or set attributes and values of the selected html elements. Syntax. For get an attribute’s value use the below syntax. $(selector).attr(attribute); Using the below syntax you can set an attribute and values. $(selector).attr(attribute,value);

Can you pass data attribute to method attribute?

EDIT: Something to note is that in order to access the data attributes of some HTML element, you must select the element via some selector (id,class,etc). You cannot pass “this” to any method attribute on the element and use that argument to access the data. It will produce an undefined.

How to access a data attribute in JavaScript?

You can access the data attribute with the property dataset, followed by the name of the data attribute. to access it, remove the data- at the beginnig of the attribute. You must also remove the hyphen. Then write the name camelCase. So data-user-name becomes userName.

You cannot pass “this” to any method attribute on the element and use that argument to access the data. It will produce an undefined. The “data-” attribute prefix should be stripped off in your .data () call, so assuming: $ (” [attr1]”) or $ (” [attr1=’this is attr 1′]”) , which will return the element (not the attribute value)

jQuery attr() Method. The jQuery attr() method is used to get or set attributes and values of the selected html elements. Syntax. For get an attribute’s value use the below syntax. $(selector).attr(attribute); Using the below syntax you can set an attribute and values. $(selector).attr(attribute,value);

EDIT: Something to note is that in order to access the data attributes of some HTML element, you must select the element via some selector (id,class,etc). You cannot pass “this” to any method attribute on the element and use that argument to access the data. It will produce an undefined.

How are attributes used in jQuery in HTML5?

HTML5 data-* Attributes. Since jQuery 1.4.3, data-* attributes are used to initialize jQuery data. An element’s data-* attributes are retrieved the first time the data() method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).