Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an HTML attribute with jQuery

Well, I have this jQuery image slideshow that uses the attribute "control" inside an <a>. Seeing how it didn't validate I searched for a way to add this attribute inside my HMTL via jQuery but I didn't really find anything relevant. Now I don't really care about how valid my page is, but I'm really curious in how to add an HTML attribute inside an HTML tag.

In case I wasn't clear enough with my explanation, I have this code:

<a id="previous" control="-6" href="#"></a>

And I want to add control="-6" with jQuery.

like image 701
Victor Ionescu Avatar asked Oct 05 '10 17:10

Victor Ionescu


People also ask

How we can add attribute in jQuery?

The <input> element contains the type and id attribute. When user click on the button, the jQuery function is called. We use $(selector). attr() method to add the attributes.

Can we use HTML in jQuery?

jQuery html() MethodThe html() method sets or returns the content (innerHTML) of the selected elements. When this method is used to return content, it returns the content of the FIRST matched element. When this method is used to set content, it overwrites the content of ALL matched elements.

How HTML elements are used in jQuery with example?

What is the use of html() method in jQuery ? The html() method in jQuery is used to get the contents of the first element in the set of matched elements or is used to set the HTML contents of every matched element. It returns the content of the first matched element. This function does not accept any arguments.


3 Answers

Use jQuery's attr function

$("#previous").attr("control", "-6");

An example

// Try to retrieve the control attribute
// returns undefined because the attribute doesn't exists
$("#previous").attr("control");

// Set the control attribute
$("#previous").attr("control", "-6");

// Retrieve the control attribute (-6)
$("#previous").attr("control");

See this example on jsFiddle


You can alternatively use data function to store values on elements. Works the same way, for example:

$("#previous").data("control"); // undefined
$("#previous").data("control", "-6"); // set the element data
$("#previous").data("control"); // retrieve -6

Using data you can store more complex values like objects

$("#previos").data("control", { value: -6 });
($("#previos").data("control")).value; // -6

See a data example on jsFiddle

like image 172
BrunoLM Avatar answered Nov 16 '22 17:11

BrunoLM


Since the jQuery version has been well covered here, I thought I'd offer something different, so here a native DOM API alternative:

document.getElementById('previous').setAttribute('control','-6');

Yes, I know you asked for jQuery. Never hurts to know the native way too. :o)

like image 38
user113716 Avatar answered Nov 16 '22 17:11

user113716


Let me see if I understood you. You have, for example, the code:

<a id="previous" href="#"></a>

And by jQuery you want it to be:

<a id="previous" control="-6" href="#"></a>

Is it right? If it is. You just have to do:

$('#previous').attr('control', -6);

If an attribute doesn't exists it's created by jQuery. So, to remove it you can do:

$('#previous').removeAttr('control');

What you're doing doesn't respect the html rules and everything else, but it works fine, a lot of plugins do the same. ;D

I hope this could be helpful!

See you!

like image 2
Jayme Tosi Neto Avatar answered Nov 16 '22 16:11

Jayme Tosi Neto