Spanky
Spanky

Reputation: 709

How can I define an attribute as a variable?

I'm trying to define the following line as a variable in Jquery by using

var attributeID = $("#element_to_pop_up").attr("attrId");

The link I am using is and attrId is a variable

<a id="element_to_pop_up" attrId="variable">Link</a>

Basically, I am trying to define the value of my attribute "attrId" as a variable.

One more thing to note This is within a FreeMarker template so FreeMarker is already giving me my variable value. I don't have to do an onclick to get the value.

Is there a better way of doing this?

Upvotes: 0

Views: 88

Answers (1)

Steve H.
Steve H.

Reputation: 6947

HTML5 supports the notion of data attributes for application-specific uses. So a better approach is to name all your attributes with the data- prefix:

<a id="element_to_pop_up" data-attrid="variable">Link</a>

To include the information that @chris97ong added, the way to access this variable data using jQuery is:

var x= $('#element_to_pop_up').data('attrid');

Upvotes: 1

Related Questions