How to add a certain value to anchor tag?

Alex G. picture Alex G. · Feb 5, 2012 · Viewed 49.1k times · Source

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 a value attribute to an anchor tag. How to do it?

Answer

Andrew Jackman picture Andrew Jackman · Feb 5, 2012

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>