+ Reply to Thread
Results 1 to 7 of 7

Thread: jQuery td onclick location change - Destination continues to be 'undefined'

  1. #1
    sikuneh is offline x10Hosting Member sikuneh is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    43

    jQuery td onclick location change - Destination continues to be 'undefined'

    On my site I am working on a jQuery script that when you click anywhere in a td (not just on the link) it will take you to the link's destination, but whenever I click it always say it is undefined which always returns a false page.

    jQuery:
    Code:
    $(document).ready(function() {
        $("table#colorNavTable tr td").click(mouseClick);
    })
     
    function mouseClick() {
        var url = $(this).next().attr("src");
        window.location = url;
        return false;
    }
    HTML:
    HTML Code:
    <table width="100" id="colorNavTable" cellpadding="2" cellspacing="2">
        <tr>
            <td><a href="page2.html">Child 1</a></td>
        </tr><tr>
            <td><a href="page2.html">Child 2</a></td>
        </tr><tr>
            <td><a href="page2.html">Child 3</a></td>
        </tr><tr>
            <td><a href="page2.html">Child 4</a></td>
        </tr>
    </table>
    Here is a link to a live site if you need it

    The destination is the same for all of them for the test. It is supposed to link to this.

    Thanks in advance.

  2. #2
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

    Re: jQuery td onclick location change - Destination continues to be 'undefined'

    1. Why did you use next() ??
    2. Why did you use attr("src")

    I believe that neither are correct here.

  3. #3
    sikuneh is offline x10Hosting Member sikuneh is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    43

    Re: jQuery td onclick location change - Destination continues to be 'undefined'

    Sorry, that was a copy error. It still doesn't work after I changed that.

    1. Why did you use next() ??
    I used next because the td tag (which I referenced with the $()) doesn't have an href attribute and if the user doesn't have javascript enabled I want them to still be able to use the links.
    Last edited by sikuneh; 05-13-2010 at 02:17 PM.

  4. #4
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

    Re: jQuery td onclick location change - Destination continues to be 'undefined'

    next() gives the next sibling element, not the next element on the page.
    The links are contained in the td elements, they are not siblings.
    Sibling means that it has the same parent.
    The parent of the td is the tr it is contained in.
    The parent of the a is the td it is contained in.

  5. #5
    sikuneh is offline x10Hosting Member sikuneh is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    43

    Re: jQuery td onclick location change - Destination continues to be 'undefined'

    That would explain a lot... but how would I get each the next a tag in the sequence?

  6. #6
    AngusThermopyle is offline x10Hosting Member AngusThermopyle is an unknown quantity at this point
    Join Date
    Nov 2009
    Posts
    84

    Re: jQuery td onclick location change - Destination continues to be 'undefined'

    http://api.jquery.com/

    You are looking for the child of the td elements.
    You are also looking for the href attribute, not the src .

    var url = $(this).children("a").attr("href");

    Since the links are the only children, you do not have to use the "a" selector, but if your html changes, it might be necessary.

    ### caution: have not tested the above code...

  7. #7
    sikuneh is offline x10Hosting Member sikuneh is an unknown quantity at this point
    Join Date
    Dec 2008
    Posts
    43

    Re: jQuery td onclick location change - Destination continues to be 'undefined'

    I took a few tutorials and it never mentioned that. Thanks, it works now.

+ Reply to Thread

Similar Threads

  1. onclick text select and also a comment script if possable
    By monsterm in forum Programming Help
    Replies: 5
    Last Post: 07-21-2009, 01:47 PM
  2. mail continues to not work
    By garrettroyce in forum Free Hosting
    Replies: 0
    Last Post: 07-26-2008, 12:55 PM
  3. CNAME Value/Destination
    By operati in forum Free Hosting
    Replies: 4
    Last Post: 06-09-2008, 12:05 AM
  4. MySQL error continues
    By moha911 in forum Free Hosting
    Replies: 23
    Last Post: 01-04-2008, 11:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers