2

In the scenario shown in the code below, how can I allow the user to click anywhere on div.post, instead of precisely on one of the the actual a elements?

<div class="post" style="padding: 20px; background-color: transparent;">

    <div class="thumbnail" style="float: left; background-color: #ccc;">    
        <a href="http://...">
            <img src="http://...">
        </a>
    </div>

    <div class="title" style="float: right;">
        <a href="http://...">title</a>
    </div>

</div>

I also need to highlight the complete div.post on mouseover, which I currently do using:

[EDIT: Thx to your answers, I have replaced the following by CSS instead of the jQuery]

$("div.post").hover(

    function() {
        $(this).css("background-color", "#333");
        $(this).find("div.thumbnail").css("background-color", "#333");
    },

    function() {
        $(this).css("background-color", "transparent");
        $(this).find("div.thumbnail").css("background-color", "#ccc");      
    }
);

EDIT: Solutions will be preferred that:

  • use CSS instead of jQuery
  • validate as XHTML 1.0 Strict
  • do not wrap an a around a div
  • do not involve an empty a

So far, I consider ArVan's solution to be the best (see my comment there).

Ben
  • 15,938
  • 19
  • 92
  • 138

9 Answers9

2

Try adding an onclick listener on the div. You can do this in the html like this <div class="post" style="padding: 20px; background-color: transparent;" onclick="location.href='http://...'">.

Or you can do it with jQuery like this:

$("div.post").click(function(){
   window.location.href = "http://...";
});
nickhar
  • 19,981
  • 12
  • 60
  • 73
ArVan
  • 4,225
  • 8
  • 36
  • 58
  • At the moment, I find using `onclick="location.href='http://...'"` to be the best answer. Of course, it would be better to have a solution that works without js, but not if it involved wrapping an `a` around a `div` or having an empty `a`. – Ben Dec 16 '11 at 12:32
  • When using this, it would be a good idea to change the `cursor` for `div.post` via css such that the user actually knows that he can click anywhere. – Ben Dec 16 '11 at 12:43
  • For cursor change just use css: `div.post{cursor:pointer;}` :-) – ArVan Dec 16 '11 at 19:22
2

Why do you need to have the DIVs at all. You could simply style the < a > tags accordingly. Add styles to < a > classes as you desire. Get rid of inline styles!

<div class="post">

        <a class="thumbnail" href="http://...">
            <img src="http://...">
        </a>

        <a class="title" href="http://...">title</a>

</div>
ckaufman
  • 1,487
  • 9
  • 15
  • Yes, I could. But your answer is not relevant to my question, in that you don't explain how I 'could simply style the < a > tags accordingly', which my question was about (-1). – Ben Dec 16 '11 at 12:47
  • Ben, if you got rid of the Div, then you wouldn't have to worry about clicking on a parent div to reference the < a > tag nested inside of it. I can better help with styling the < a > tag if you reference how you plan on styling them (not in your original post). All of your inline styling could be applied through CSS referencing ID tags for each < a >. – ckaufman Dec 19 '11 at 02:21
1

You could of course also make the a elements fill up the entire div it's in - and set the background to the a element - so you'll only need js for .post background:

a {
    background:transparent;
    display:block;
    height:100%; /* .post will need a fixed height */
    width:100%;
}
a:hover {
    background:#333;
}
ptriek
  • 9,040
  • 5
  • 31
  • 29
1

The only reason that you wouldn't want to wrap a <div> tag with an <a> tag is because you're not supposed to put block-level elements inside inline elements. If you set your containing <a> tag to be a block-level element, then there is no reason why you wouldn't want to wrap the <div> with an <a> tag.

Also, keep in mind that you can place <a> tags within <a> tags.

<a class="post" href="#" style="padding: 20px;">
    <div class="thumbnail" style="float: left; background-color: #ccc;">    
        <a href="http://...">
            <img src="http://...">
        </a>
    </div>
    <div class="title" style="float: right;">
        <a href="http://...">title</a>
    </div>
</a>
Wex
  • 15,539
  • 10
  • 64
  • 107
0

You can choose to keep or remove the links, but all you have to do is use a jQuery click function on the div, and window.location.

$("div.post").click(function(){
    window.location.href = "myHrefHere.ext";
});

Super easy!

TaylorPLM
  • 101
  • 1
  • 1
  • 10
0

Heres a DEMO

$('div > a').each(function() {
    var href = this.href;
    $(this).parent().click(function() { location.href = href; });
});
qwertymk
  • 34,200
  • 28
  • 121
  • 184
0

If you're using HTML5 you can give the anchor tag some children and use CSS accordingly. This requires the least amount of css and markup and no JS at all.

Check out this example: http://jsfiddle.net/DavidVII/6ErgJ/

also, this validates using HTML5. This of course isn't legal in strict.

DavidVII
  • 2,133
  • 2
  • 21
  • 28
  • Are we assuming everyone already uses HTML5 now? ;) As you said, I don't believe this validates strict... – Ben Dec 16 '11 at 11:49
  • 1
    That's why I mentioned it. I'm not assuming anything. In fact, I'm pretty sure I said, "IF you're using HTML5." Anyways, this works fine in HTML5. – DavidVII Dec 16 '11 at 19:57
  • Also, I think it's garbage I got down voted for posting an HTML5 option. Poop sauce... – DavidVII Jan 23 '13 at 01:26
0

You can do it just with CSS. This is a mix of @ptriek's and @qwertymk's solutions.

demo

Remember, if you're going to be floating stuff, you have to clear: both; after it. Try removing the clearfix, and see how it's different.

Update:

For a little extra credit, I replaced the jQuery for hovering with CSS. I think it works pretty well. The demo link above has been updated.

Update 2:

Here's a bit of a compromise. A little jQuery is used to let the anchor go inside the div, instead of around it. It all validates as XHTML 1.0 Strict and works fine. I also added cursor: pointer; to make it look like a link (you get the usual hand cursor for links) even though it's a div.

The XHTML looks like this,

<div class="post" style="padding: 20px;">
    <a href="http://..." class="placehold" />
    <!-- content goes here -->
</div>

You can still highlight any text inside the div, and right click images (to copy or save-as).

http://jsfiddle.net/HXdA5/4/

Brigand
  • 84,529
  • 20
  • 165
  • 173
  • Thx, I wasn't aware that `.post:hover .thumbnail` would be possible. In this case, I can indeed replace my jQuery for the highlighting with CSS (+1). For your demo, I don't believe wrapping an `a` around a `div` is valid (-1). – Ben Dec 16 '11 at 12:04
  • @Ben, it's valid in HTML5. Before that (4.x) the W3C doesn't say it's valid, but it works everywhere as long as you set it to `display: block;`. – Brigand Dec 16 '11 at 13:28
  • @Ben, I added some more changes that work better, and are valid XHTML. See "Update 2" in my answer, and this [updated fiddle](http://jsfiddle.net/HXdA5/4/). – Brigand Dec 16 '11 at 14:55
-1

Or, you could avoid using Javascript at all (which I always prefer when possible), by positioning your link over your entire div.post element, and using CSS hover effects:

http://jsfiddle.net/jblasco/WXugG/14/

At the very least you should use CSS for changing the background color on hover, rather than Javascript.

EDIT: Updated the fiddle to include an empty span in the a element, as per Make a div into a link.

LATEST EDIT: Updated the fiddle to use the XHTML 1.0 Strict doctype here, which both works and validates successfully.

Community
  • 1
  • 1
justisb
  • 7,081
  • 2
  • 26
  • 44
  • @Ben What's the issue with that, exactly? – justisb Dec 16 '11 at 12:46
  • I believe the content inbetween `` and `` should be descriptive of the location referred to in the `href` attribute. Also: "Note. User agents should be able to find anchors created by empty A elements, but some fail to do so." (from http://www.w3.org/TR/html4/struct/links.html#h-12.3.3) – Ben Dec 16 '11 at 12:59
  • You could add an empty `span` element in the `a`, and apply the `a`'s styles to it, instead. I believe that's actually the preferred solution, for those browsers that ignore an empty `a` element. – justisb Dec 16 '11 at 13:07
  • Also I set the fiddle to use the HTML 4.01 Strict doctype, and it validates if you try it at http://jsfiddle.net/jblasco/WXugG/6/show/ or check http://tinyurl.com/cou5wkl directly. Is there any other reason you would prefer not to use this solution? – justisb Dec 16 '11 at 13:21
  • Also this validates as XHTML 1.0 Strict, check http://jsfiddle.net/jblasco/WXugG/14/show/ or http://tinyurl.com/6tn9hum – justisb Dec 17 '11 at 20:57