Possible Duplicate:
How to add new class and attributes to div if it exists on the page
I need JavaScript code on my master page which tries to find if a div exists. If so, it should add a new class and also add a new id attribute.
For example if the page has this div:
<div class="toplink">abc..</div>
Then JavaScript code should make it exactly like this:
<div class="toplink adin" data-aid="114">abc..</div>
The code inside the div should remain the same.
I tried this code but this is not working
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.js" type="text/javascript">
if ($('.toplink').exists()) {
$('.toplink').addClass('adin').attr('data-aid', '114');
}
</script>
What wrong with this code? what where i placed this code,in header on footer?
Can i do this with javascript, not jquery
Try $().length property instead, and place this snippet at the very bottom of the page, before closing body tag