0

I'm ultimately trying to find all image tags on a page and print out the ones without alt tags.

Right now I have this:

var str = '<img id="img1" /><img id="img2" /><img id="img3" /><img id="img4" alt="ASDF" title="" /><img id="img5" alt="" /><img id="img6" title="" />';
var imagesWithoutAlt = $( str ).filter( 'img:not([alt])' );
var newString = new String( $( imagesWithoutAlt ).clone().html());
$(document.body).append($('<textarea rows="6" cols="40"/>').text( newString );

But this is failing to output anything. Help?

2
  • 2
    you need to wrap the imagesWithoutAlt clone in an element and get that element's html. Commented May 3, 2012 at 18:01
  • OK. I'm new to jQuery, how would I do that? Commented May 3, 2012 at 18:11

1 Answer 1

3

Working Demo: http://jsfiddle.net/pkdtg/

var str = '<img id="img1" /><img id="img2" /><img id="img3" /><img id="img4" alt="ASDF" title="" /><img id="img5" alt="" /><img id="img6" title="" />';
var imagesWithoutAlt = $(str).filter('img:not([alt])');
var newString = $(imagesWithoutAlt).clone().wrapAll("<div />").parent().html();
$(document.body).append($('<textarea rows="6" cols="40"/>').text(newString));​

I added .wrapAll("<div />").parent()

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the prompt help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.