Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

[RESOLVED] JQuery not finding the new elements

Ecniv

Don't Panic!
Joined
Nov 17, 2000
Messages
5,343
Ok, so a page I'm working on has a grid (table) AJAXed in/updated.

The check box on the grid JQuery can find, however the td with unique id's it cannot find.
I made some javascript/jquery to create a string of the id, then put it into $(sIdToFind) ... which returns the JQuery object, but its null.
I checked on Chrome with the inspector, and the element is there with the ID. But JQuery can't see it.
document.getElementById however can.

Is there something I need to do with the JQuery to get it to re-read the DOM ?
 

KGComputers

New member
Joined
Dec 7, 2005
Messages
1,949
Re: JQuery not finding the new elements

Given the table:

[highlight=vb]<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td id="tdMaria">Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td id="tdFrancis">Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td id ="0_table0_cell_c0_r1">Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
[/highlight]

I can select the td's using their unique id with the following selectors below. For demo purposes, I just need to include the text() method to read the text inside the td element.

[highlight=vb]
$(document).ready(function () {

var td = $("td[id='tdMaria']").text();
alert(td);

var td1 = $('#tdFrancis').text();
alert(td1);

alert($('#0_table0_cell_c0_r1').text());

alert($("td[id='0_table0_cell_c0_r1']").text());
});
[/highlight]

If none of the provided selectors work, maybe you can show us a fiddle or the url itself.

- kgc
 
Last edited:

Ecniv

Don't Panic!
Joined
Nov 17, 2000
Messages
5,343
Re: JQuery not finding the new elements

Hi

Can't show it as the table is generated after the page and inserted.

I got around it by using the JQuery on the container element listening for the type of element. This gets the ID of the row, used this to build the name of the id of the td I wanted.

Here Jquery wouldnt find it, but document.getelementbyid did. So I reverted to that to get the innerHTML of the element. Convert to a number and add to a holding total. Then throw that result with a number format on top to a display element.

Works...


Just wondering if there is any way at all that the JQuery can be told to go get the DOM now, rather than just at the time of first execution...
Or whether it's been fixed since the version that is currently being used (1.7). I think the company is going to update to 1.12 soon though, so will have to re-test again after that...

Thanks for the suggestion.
 

szlamany

MS SQL Powerposter
Joined
Mar 5, 2004
Messages
17,872
Re: JQuery not finding the new elements

jQuery always finds id's in the current DOM - it's not storing something based on first execution.

Are you using something like FireFox to debug? If not you should be doing so and then we can talk further about what your DOM has "live" for elements.

I always build my page with jQuery - sometimes even grabbing HTML from SQL select statements. I can always find new elements or it would be a useless tool.

And I'm using 1.7 as well...

Might you have ID's that are NOT unique on your page??
 

Ecniv

Don't Panic!
Joined
Nov 17, 2000
Messages
5,343
Re: JQuery not finding the new elements

Even if they weren't unique, they come back in the jquery collection?

Its weird, because usually JQuery has been fine finding anything. I only encountered problems with listeners for objects not created (which is fine, I can work around that). But elements that are unique name/id ed.

Only with the inserted html. If they are on the page already, it can find it.


I'll play with it some more soon and see if it happening all the time with inserted html, or just on the grid/table. Maybe there is a problem on the table generation...

Will post back later.
 

szlamany

MS SQL Powerposter
Joined
Mar 5, 2004
Messages
17,872
Re: JQuery not finding the new elements

Non unique ID's are evil and cause unpredictable results - might come back - might not.

If you find something that you can easily repeat that shows this happening I would be willing to check it out. My clients rely heavily on jQuery working for them!
 
Top