Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Apr 2010
    Posts
    125
    Thanks
    6
    Thanked 0 Times in 0 Posts

    JQuery "trigger" won't work. What am I doing wrong?

    Thank you everyone for your help with my jquery problem. I now have two working pieces of code:

    Code:
    <div id="myselector"><a href="/joke.php"><img src="/images/joke.gif" border="0" /></a></div>
    <script>
    $(document).ready(function() {
    $("#myselector").mouseover(function() {window.location.href = $(this).find('a').attr('href');});
    });
    </script>
    and:

    Code:
    <div id="myselector"><a href="/joke.php"><img src="/images/joke.gif" border="0" /></a></div>
    <script>
    $( document ).ready(function() {
    $("#myselector").mouseover(function( event ) { 
     $(this).find('a')[0].click(); 
    });
    });
    </script>
    Which fires the /joke.php link when the image is hovered over. But this is going to be a sitewide code for various old elements. Some, are called by javascript. And this:

    Code:
    <div id="myselector"><script type="text/javascript" src="/archive/humour.js"></script></div>
    <script>
    $( document ).ready(function() {
    $("#myselector").mouseover(function( event ) { 
     $(this).find('a')[0].click(); 
    });
    });
    </script>
    does not work. The humour.js file returns a random jpg/gif that has an <a href> link. How do I get the href link in the javascript file to fire on hover?

    Thanks again
    Last edited by instaunt; 03-05-2013 at 02:12 AM.
    I've been making animations for my daughter. Tell me what you think!

    Pickles and the Bully
    Where are you Pickles!?!

  • #2
    Regular Coder
    Join Date
    Apr 2010
    Posts
    125
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I should probably add:

    This is my first jquery attempt! PHP and moderate Javascript are all I generally do.
    I've been making animations for my daughter. Tell me what you think!

    Pickles and the Bully
    Where are you Pickles!?!

  • #3
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Some comments

    1 - why do you expect that anything happens on hover? You did not specify any mouseover/mouseout or mouseenter/mouseleave handlers ...
    2 - The script part that uses the selector '#myselector' is placed before the element with id="myselector". At that point jQuery cannot find the element because it is literally not there yet. So either move the script after the element or use $(document).ready(function() { // your code here });

  • #4
    Regular Coder
    Join Date
    Apr 2010
    Posts
    125
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Am I getting any closer???

    Code:
    <script src="/js/jquery-1.9.1.min.js"></script>
    <div id="myselector"><a href="/joke.php"><img src="/images/joke.gif" border="0" /></a></div>
    <script>
    $( document ).ready(function() {
    $(#myselector).mouseenter(function() { $(this).click() });
    });
    </script>
    I've been making animations for my daughter. Tell me what you think!

    Pickles and the Bully
    Where are you Pickles!?!

  • #5
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    you could maybe try $(this).find('a').click()

  • #6
    Regular Coder
    Join Date
    Apr 2010
    Posts
    125
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DanInMa View Post
    you could maybe try $(this).find('a').click()
    Thank you, but no that didn't work. I assume you mean this:

    Code:
    <div id="myselector"><a href="/joke.php"><img src="/images/joke.gif" border="0" /></a></div>
    <script>
    $( document ).ready(function() {
    $(myselector).mouseover(function() { $(this).find('a').click() });
    });
    </script>
    I've been making animations for my daughter. Tell me what you think!

    Pickles and the Bully
    Where are you Pickles!?!

  • #7
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    Code:
    <div id="myselector"><a href="/joke.php"><img src="/images/joke.gif" border="0" /></a></div>
    <script>
    $(document).ready(function() {
    $("#myselector").mouseover(function() {window.location.href = $(this).find('a').attr('href');});
    });
    </script>
    I suppose you could do it like this.

  • Users who have thanked DanInMa for this post:

    instaunt (03-05-2013)

  • #8
    Senior Coder DanInMa's Avatar
    Join Date
    Nov 2010
    Location
    Salem,Ma
    Posts
    1,577
    Thanks
    13
    Thanked 248 Times in 248 Posts
    http://unitstep.net/blog/2010/04/12/...-using-jquery/
    this still applies apparently. I use jQuery for all kinds of things, never realized something so simple wouldn't work the way you think it would.

  • Users who have thanked DanInMa for this post:

    instaunt (03-05-2013)


  •  

    Posting Permissions

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