Alex G. Alex G. - 6 months ago 12
Javascript Question

How to add a certain value to anchor tag?

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

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>