PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
[RESOLVED] JQuery not finding the new elements-VBForums
Results 1 to 6 of 6

Thread: [RESOLVED] JQuery not finding the new elements

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Resolved [RESOLVED] JQuery not finding the new elements

    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 ?

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    1,948

    Re: JQuery not finding the new elements

    Given the table:

    html Code:
    1. <table>
    2.     <tr>
    3.         <th>Company</th>
    4.         <th>Contact</th>
    5.         <th>Country</th>
    6.     </tr>
    7.     <tr>
    8.         <td>Alfreds Futterkiste</td>
    9.         <td id="tdMaria">Maria Anders</td>
    10.         <td>Germany</td>
    11.     </tr>
    12.     <tr>
    13.         <td>Centro comercial Moctezuma</td>
    14.         <td id="tdFrancis">Francisco Chang</td>
    15.         <td>Mexico</td>
    16.     </tr>
    17.     <tr>
    18.         <td>Ernst Handel</td>
    19.         <td>Roland Mendel</td>
    20.         <td>Austria</td>
    21.     </tr>
    22.     <tr>
    23.         <td>Island Trading</td>
    24.         <td>Helen Bennett</td>
    25.         <td>UK</td>
    26.     </tr>
    27.     <tr>
    28.         <td>Laughing Bacchus Winecellars</td>
    29.         <td>Yoshi Tannamuri</td>
    30.         <td id ="0_table0_cell_c0_r1">Canada</td>
    31.     </tr>
    32.     <tr>
    33.         <td>Magazzini Alimentari Riuniti</td>
    34.         <td>Giovanni Rovelli</td>
    35.         <td>Italy</td>
    36.     </tr>
    37. </table>

    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.

    JavaScript Code:
    1. $(document).ready(function () {
    2.  
    3.     var td = $("td[id='tdMaria']").text();  
    4.     alert(td);
    5.  
    6.     var td1 = $('#tdFrancis').text();
    7.     alert(td1);
    8.  
    9.     alert($('#0_table0_cell_c0_r1').text());
    10.  
    11.     alert($("td[id='0_table0_cell_c0_r1']").text());
    12. });

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

    - kgc
    Last edited by KGComputers; Oct 20th, 2016 at 06:17 AM.
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal |Programming | GitHub
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    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.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,866

    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??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    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.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    CT
    Posts
    17,866

    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!

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width