Using <div onclick="""> to call an <a></a> link

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: May 2009
Posts: 10
Reputation: MattTheHat is an unknown quantity at this point 
Solved Threads: 0
MattTheHat MattTheHat is offline Offline
Newbie Poster

Using <div onclick="""> to call an <a></a> link

 
0
  #1
Jul 3rd, 2009
Is it possible to do something like this?

<div id="btn1" onclick="//somehow call page2Link here"><a href="page2.html" id="page2Link">Page 2</a></div>
Basically it's to try and make the text work with the a:hover text effect I have whilst making the whole layer sensitive to clicks.

i tried this already:

<a href="page2.html" id="page2Link"><div id="btn1" ><a href="page2.html" id="page2Link">Page 2</a></div></a>
but it's untidy and a bit rubbish is there a better way to do it neatly?

I know i could always call a JS function to do a parent.location thing but i would rather not if possible.
Last edited by peter_budo; Jul 4th, 2009 at 6:05 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 171
Reputation: Menster is an unknown quantity at this point 
Solved Threads: 22
Menster's Avatar
Menster Menster is offline Offline
Junior Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #2
Jul 3rd, 2009
Hi there, have you tried:
<div id="blah" onclick="window.location = 'Your reference here'>
<a href="You can leave this blank">Your text here</a>
</div>
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 10
Reputation: MattTheHat is an unknown quantity at this point 
Solved Threads: 0
MattTheHat MattTheHat is offline Offline
Newbie Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #3
Jul 3rd, 2009
cheers i'll give that a go
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 13
Reputation: Bojero is an unknown quantity at this point 
Solved Threads: 3
Bojero Bojero is offline Offline
Newbie Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #4
Jul 3rd, 2009
You can leave out the <a> tag then because you can just use the div:hover.

  1. <div id="myButton" onclick="window.location = 'http://google.com';">Page 2</div>
Last edited by Bojero; Jul 3rd, 2009 at 7:04 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 171
Reputation: Menster is an unknown quantity at this point 
Solved Threads: 22
Menster's Avatar
Menster Menster is offline Offline
Junior Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #5
Jul 3rd, 2009
But, if you leave out the anchor tag the styles you've applied won't work... but i suppose you could just change them from a:hover etc.
to div:hover.
$me = new Person();
if (isset($_COOKIE)){
$me->eat($_COOKIE);
} else { $me->starve(); }
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 178
Reputation: codejoust is an unknown quantity at this point 
Solved Threads: 17
codejoust's Avatar
codejoust codejoust is offline Offline
Junior Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #6
Jul 6th, 2009
What I end up doing quite a bit, is making the div clickable, but allowing for people without javascript to access the link (such as search spiders).
One way to do this is using jquery --
  1. $(function(){
  2. $('div.link:has(a)').click(function(){
  3. $(this).addClass('linked'); /*Allows for CSS hover and different styles for link clicks.*/
  4. window.location = $(this).nearest('a').attr('href'); /*Go there! Only works with jquery 1.3 (due to the use of nearest. */
  5. }); //End Click
  6. }); //End OnReady
Which allows for the div to be clickable.
Last edited by peter_budo; Jul 8th, 2009 at 7:20 am. Reason: Correcting openning tag please use [code=JavaScript] instead of [code language=JavaScript]
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 13
Reputation: Bojero is an unknown quantity at this point 
Solved Threads: 3
Bojero Bojero is offline Offline
Newbie Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #7
Jul 6th, 2009
Originally Posted by codejoust View Post
What I end up doing quite a bit, is making the div clickable, but allowing for people without javascript to access the link (such as search spiders).
One way to do this is using jquery --
[code language=javascript]
$(function(){
$('div.link:has(a)').click(function(){
$(this).addClass('linked'); /*Allows for CSS hover and different styles for link clicks.*/
window.location = $(this).nearest('a').attr('href'); /*Go there! Only works with jquery 1.3 (due to the use of nearest. */
}); //End Click
}); //End OnReady
[/code]
Which allows for the div to be clickable.
Mmmm jQuery!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 10
Reputation: MattTheHat is an unknown quantity at this point 
Solved Threads: 0
MattTheHat MattTheHat is offline Offline
Newbie Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #8
Jul 6th, 2009
Thanks to all.

Problem now solved.


ps. Give JQuery a rest some of us like to code in an old school manner where all the code is ours rather than being cut & paste monkeys.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 178
Reputation: codejoust is an unknown quantity at this point 
Solved Threads: 17
codejoust's Avatar
codejoust codejoust is offline Offline
Junior Poster

Re: Using <div onclick="""> to call an <a></a> link

 
0
  #9
Jul 6th, 2009
Originally Posted by MattTheHat View Post
Thanks to all.

Problem now solved.


ps. Give JQuery a rest some of us like to code in an old school manner where all the code is ours rather than being cut & paste monkeys.
I wrote the jQuery javascript, It isn't cut+paste from somewhere else, but I do understand liking to use the 'old-school' methods... not much problem there, although jQuery (I think) helps it so you don't need to change any HTML to get the effect...
Its also sometimes hard to relearn something too...
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC