CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript frameworks (http://www.codingforums.com/forumdisplay.php?f=62)
-   -   JQuery "trigger" won't work. What am I doing wrong? (http://www.codingforums.com/showthread.php?t=288766)

instaunt 03-04-2013 02:09 PM

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

instaunt 03-04-2013 02:28 PM

I should probably add:

This is my first jquery attempt! PHP and moderate Javascript are all I generally do.

devnull69 03-04-2013 03:20 PM

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 });

instaunt 03-04-2013 07:50 PM

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>


DanInMa 03-04-2013 08:08 PM

you could maybe try $(this).find('a').click()

instaunt 03-04-2013 08:44 PM

Quote:

Originally Posted by DanInMa (Post 1317591)
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>


DanInMa 03-05-2013 12:25 AM

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.

DanInMa 03-05-2013 12:38 AM

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.


All times are GMT +1. The time now is 04:44 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.