How to add a specific value for tag binding?
I have the following code
<a href="" (set value 1)>Inside Link which sets a value</a> <script> $(a).click(function() { i=value of a tag; $('#square').animate({'left': i * 360}); }); </script> And I want to add the value attribute to the anchor tag. How to do it?
If you want to add a random attribute for a value, you can use data attributes:
<a href="#" data-value="1">Text</a> <script type="text/javascript"> $("a").click(function(){ i=$(this).data("value"); $('#square').animate({'left': i * 360}); }); </script> If you are using HTML5, you can use the data- technique.
<a id="target" href="http://foo.bar" data-custom-value="1">Text</a> $("#target").click(function() { var value = $(this).data("custom-value"); // do other stuff. }); EDIT
Using .data instead of .attr more suitable
you can use special data attributes, see this .
<a href="#" data-json="{ 'myValue':'1'}">Click</a> //you can even pass multiple values there. then enter it using the data () function.
Or instead of using json, you can put it as an attribute:
<a href="link" myvalue="1""> then get it using:
$("#link").data("myvalue") <a href="#" data-value="IE" id="click">Click</a>
` $("#click").click(function(event){console.log($(this).data("value"));});` data-value is a good attribute. but .. You can also add the "rel" attribute to your anchor tag. it describes the relationship to the document referenced. And you can also use it to store values.
Like this -
$("a").click(function(){ var page = $(this).attr('rel'); // save the attribute value here sessionStorage.setItem("text",page); /*save it in session storage if you want to send (or retrieve) this value to another page. if not then use it easily without saving it in session storage*/ //Use it here return false; // to stop the redirection if <a> contains a link });