0

I have a very simple problem and keep finding answers to similar questions with more complexity. I am trying to replace image links in loaded html and decided that the best is to read the html into a string variable loadedHTML using .get(), like this:

$.get(loadURL, function(loadedHTML) {
    myFunction(loadedHTML);
}, 'html');

In myFunction, I want to make some changes to the loaded html and eventually return it. I can't get .find() to work. Here is what that code looks like:

function myFunction( html ) {
    var $html = $("<div>" + html + "</div>");
    console.log( "$html.html() = " + $html.html()); // works!
    $html.find("img", function() {
        console.log("found an image"); // doesn't work :(
    });
}

I am killing myself with something that is probably really simply. Let me know how I am dumb please...

1
  • find() doesn't have a callback, but might be something worth extending Commented May 5, 2014 at 12:40

6 Answers 6

3

I'm almost sure that you cannot use find in the way that you have.

Try something like:

var $foundImages = $html.find("img");
console.log($foundImages.length);

Which would, in theory, output the number of images that were found.

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

2 Comments

The problem is that I need to edit those elements' attributes, not just see how many there are.
Then use $.each() to go through each one.
1

The find method doesn't have a second parameter:

http://api.jquery.com/find/

You should try this:

function myFunction( html ) {
    var $html = $("<div>" + html + "</div>");
    console.log( "$html.html() = " + $html.html()); // works!
    console.log($html.find("img"));
}

Comments

1

Simply assign id to your div tag .

like below,

 var $html = $("<div id='placeholder'>" + html + "</div>"); 

and find img with it like below,

$("#placeholder").find("img", function() {
    console.log("found an image"); // doesn't work :(
});

your resultant code,

function myFunction( html ) {
var $html = $("<div id='placeholder'>" + html + "</div>");
console.log( "$html.html() = " + $html.html()); // works!
$("#placeholder").find("img", function() {
    console.log("found an image"); // doesn't work :(
});

}

Comments

0

.find() didn't have callback function in jquery. it have parameter for selectors,elements,jqueryObject only.you have to check with length or condition like this

if($html.find("img").length > 0){

      // do stuff here
}

or

  if($html.has("img")){

       // do stuff here
   }

Comments

0

You can use this .filter():

var found = $html.find("img").filter(function() {
    return this;
});
console.log(found);

or make an array out of it with .map():

var found = $html.find("img").map(function() {
    return this;
}).get(); // use get() method here to get the values in array
console.log(found.length); // would give you the length of array created.

Comments

0

jQuery.find() doesn't have a callback but you can extend jQuery to do what you want:

jQuery.fn.extend({
    findEach: function (selector, callback) {
        var found = this.find(selector);

        if (typeof callback == 'function' && found.length > 0) {
            found.each(callback);
        }
        return found;
    }
});

Then use like you expect:

$html.findEach("img", function(key, value) {//will run for each image
    console.log(key);
    console.log(value);
    console.log(this);
});

Comments

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.