views:

34

answers:

1

Ok I'm submitting a form via ajax. The result string is a bunch of html links and images which I append to the body of the DOM. Is it possible to search with jQuery within this new code?

before ajax request:

<div id="results"></div>

after ajax request:

<div id=results"> 
  <img src="somevalidpicture.jpg" class="image"/>
  <img src="somevalidpicture2.jpg" class="image"/>
</div>

How can I run my jQuery code to manipulate that code again? For example:

$(".image").show(slow);

I suspect I cannot access the .image class, because it wasn't there when the page first loaded. And it doesn't show up in the sourcecode (browser). Is there a way to refresh/update the DOM?

+1  A: 

You can access them, as soon as the elements are added to the DOM $(".image") will find them. However slow needs to be in quotes for your particular example:

$(".image").show("slow");

"show" is a key that means 600ms, $(".image").show(600); would have the same effect.


Don't use "View Source" to see what's currently in your page (since it'll show what was there when the page loaded), instead use a DOM inspector like Firebug or the Chrome Developer tools.

Nick Craver
Thanks, you're right - that line was just out of my head. I wanted to know where I have to place my $(".image") command? Does it have to be after the $.ajax() part or somewhere particular?
j. frakes
Or a better question, is the ready event called again, after an ajax request?
j. frakes
The $(".image").show('slow') needs to be in a callback function. Typically, it would looks something like this:$.ajax({ ..., success : function(){ $(".image").show("slow") } });
Horatio Alderaan
That works for me with classes or tags which were there before I did the request, but newly added aren't recognized somehow
j. frakes
I can't even hide them with $('img').hide(); on $.ajax{ success:... } or .ajaxComplete eventhough those events get called
j. frakes
@jfrakes - make sure you're calling `$(".image")` *after* the elements are added.
Nick Craver
I edited my original post to show my code
j. frakes