Go Back   CodingForums.com > :: Client side development > JavaScript programming > JavaScript frameworks

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-04-2013, 02:09 PM   PM User | #1
instaunt
Regular Coder

 
Join Date: Apr 2010
Posts: 101
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
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
__________________
I've been making animations for my daughter. Tell me what you think!

Pickles and the Bully
Where are you Pickles!?!

Last edited by instaunt; 03-05-2013 at 01:12 AM..
instaunt is offline   Reply With Quote
Old 03-04-2013, 02:28 PM   PM User | #2
instaunt
Regular Coder

 
Join Date: Apr 2010
Posts: 101
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
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!?!
instaunt is offline   Reply With Quote
Old 03-04-2013, 03:20 PM   PM User | #3
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,261
Thanks: 10
Thanked 533 Times in 527 Posts
devnull69 will become famous soon enough
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 });
devnull69 is offline   Reply With Quote
Old 03-04-2013, 07:50 PM   PM User | #4
instaunt
Regular Coder

 
Join Date: Apr 2010
Posts: 101
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
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!?!
instaunt is offline   Reply With Quote
Old 03-04-2013, 08:08 PM   PM User | #5
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,336
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
you could maybe try $(this).find('a').click()
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Old 03-04-2013, 08:44 PM   PM User | #6
instaunt
Regular Coder

 
Join Date: Apr 2010
Posts: 101
Thanks: 4
Thanked 0 Times in 0 Posts
instaunt is an unknown quantity at this point
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!?!
instaunt is offline   Reply With Quote
Old 03-05-2013, 12:25 AM   PM User | #7
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,336
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
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.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
instaunt (03-05-2013)
Old 03-05-2013, 12:38 AM   PM User | #8
DanInMa
Senior Coder

 
DanInMa's Avatar
 
Join Date: Nov 2010
Location: Salem,Ma
Posts: 1,336
Thanks: 13
Thanked 207 Times in 207 Posts
DanInMa is on a distinguished road
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.
__________________
- Firebug is a web developers best friend! - Learn it, Love it, use it!
- Validate your code! - JQ/JS troubleshooting
- Using jQuery with Other Libraries - Jslint for Jquery/other JS library users
DanInMa is offline   Reply With Quote
Users who have thanked DanInMa for this post:
instaunt (03-05-2013)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:53 AM.


Advertisement
Log in to turn off these ads.