Tuesday, 6 August 2013

Select all links with certain elements

Select all links with certain elements

I want to select the a tag under some divs
my html
<div class='test'>
<div>
<div>
<a href='#'> bunch of stuff1 </a>
<a href='#'> bunch of stuff2 </a>
<a href='#'> bunch of stuff3 </a>
</div>
</div>
</div>
I want to replace the above html to
<div class='test'>
<div>
<div>
<a href='#' class='beam' onclick='return false;'> bunch of stuff1
</a>
<a href='#' class='beam' onclick='return false;'> bunch of stuff2
</a>
<a href='#' class='beam' onclick='return false;'> bunch of stuff3
</a>
</div>
</div>
</div>
my codes:
var texts =$('.test')
texts.children().children().find('a').each(function(){
var text = $(this).text();
$(this).replaceWith("<a href='#' class='beam' onclick='return
false;'>" + text + "</a>");
})
The above codes don't seem to work. I can't change any of the html
structure nor giving those div id or class. Thanks for the help!

No comments:

Post a Comment