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?
$("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.
});
This should do it:
$('a[id^=a]').each(function() { $(this).replaceWith($(this).html()); });
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With