Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stripping out a link in jQuery

I have a bit of html like so:

<a href="#somthing" id="a1"><img src="something" /></a>
<a href="#somthing" id="a2"><img src="something" /></a>

I need to strip off the links so I'm just left with a couple of image tags. What would be the most efficient way to do this with jQuery?

like image 684
defrex Avatar asked Oct 08 '08 22:10

defrex


3 Answers

$("a > img").parent()   // match all <a><img></a>, select <a> parents
   .each( function()    // for each link
   { 
      $(this).replaceWith(              // replace the <a>
         $(this).children().remove() ); // with its detached children.
   });
like image 142
Shog9 Avatar answered Sep 29 '22 19:09

Shog9


This should do it:

$('a[id^=a]').each(function() { $(this).replaceWith($(this).html()); });
like image 35
Andrew Moore Avatar answered Sep 29 '22 18:09

Andrew Moore


In plain javascript it would be something like:

<script type="text/javascript">
window.onload = function(){
  var l = document.getElementsByTagName("a");
  for(i=0, im=l.length; im>i; i++){
    if(l[i].firstChild.tagName == "img"){
      l[i].parentNode.replaceChild(l[i].firstChild,l[i]);
    }
  }
}
</script>
like image 36
roenving Avatar answered Sep 29 '22 19:09

roenving