views:

217

answers:

2

I want to make this div click-able and want to use same href of inside <a> and want to keep link in inside <a> also (so if JS is disabled then link will be accessible).

<div id="example">

<p> some text </p>

<img src="example.jpg />

<a href="http://stackoverflow.com"&gt; link </link>

</div>

I mean i need like this.

<div id="example" href="http://stackoverflow.com"&gt;

<p> some text </p>

<img src="example.jpg />

<a href="http://stackoverflow.com"&gt; link </link>

</div>
+2  A: 

Something like this should do

$('#example').click(function() {
  window.location.href = $('a', this).attr('href');
});
Ben Rowe
To add to this, having the `cursor: pointer;` css style on the `#example` div will give the users feedback that they *can* click the div.
Josiah
@Josiah agreed, good point
Ben Rowe
`return false;`
SLaks
@Ben Rowe - Great help Thanks. It's working. http://jsbin.com/acazi/2
metal-gear-solid
@Josiah +1 thanks for good point of usability. The only cons of this trick is it's doesn't show link status in status bar upon mouse over on `div`.
metal-gear-solid
@Slaks - What do you mean by `return false;`? should i add this in code?
metal-gear-solid
Yes, you probably should.
SLaks
@SLaks - but where? do you mean like this `$('#example').click(function() { window.location.href = $('a', this).attr('href'); return false;});` What is the benefit to add this?
metal-gear-solid
@Ben Rowe - how to do this for multiple `div`? I mean how to add more `div` here `$('#example')`? Can i add more `div` like this `$('#example, #example2, #example3 ')`
metal-gear-solid
@metal-gear-solid yeah you can expand the selector to include other divs in that way
Ben Rowe
You can also use `window.status` to affect the hover over status if you want, however it only works in IE unfortunately.
Ben Rowe
+2  A: 

I cannot reply to the above conversation yet because I am new here, but I just wanted to say that you do not need to return false from this function. What that does is prevent the default behavior of the event from happening (which, in the case of clicking a div is nothing) and prevent the event from propagating up to the next element in the DOM hierarchy.

It is unlikely that either of these are necessary for this case. Additionally, if you do need these behaviors, you can get them separately and more clearly like so:

// note that the function will be passed an event object as
// its first argument.
$('#example').click(function(event) { 
  event.preventDefault(); // the first effect
  event.stopPropagation(); // the second

  window.location.href = $('a', this).attr('href');
});

Again, I don't think either approach is necessary in this case, but it is important to understand how they work, because you will need one or both of them frequently.

Kent
Unless the user clicks in the `<a>`.
SLaks
Correct, but the event handler here is bound to the `<div>`, not the `<a>`, so the `return false;` would not take effect in time to preventDefault on the `<a>`. Actually, this is always true within jQuery, which, as far as I know, always uses the bubbling model, but in normal DOM scripting, the `<div>`'s handler could fire first in W3C compliant browsers when using capturing-style event propagation. In any case, it wouldn't make much of a difference for this specific case.
Kent