views:

28

answers:

3
// clickable blocks
$(".product").click(
function () {
    window.location = $(this).find('a').attr("href").css("cursor", "pointer");
    return false;
});

The container is made clickable but the cursor remains the same. Why isn't the css selector working?

+2  A: 

The return value from ".attr" is the attribute value, not a jquery object.

$(".product").click(function () {
  window.location = $(this).find('a').attr("href");
  $(this).find('a').css("cursor", "pointer");
  return false;
});

If you want the "container" to have a new cursor, then maybe you want this:

$(".product").click(function () {
  window.location = $(this).find('a').attr("href");
  $(this).css("cursor", "pointer");
  return false;
});
Pointy
Still no effect.
Nimbuz
That's because it will only have the cursor on click.
D_N
A: 

Do you really want to set the cursor in the click block? It seems to me that to do what you really want, you need this instead:

Edit: OK, taking into account that you only want to set click events on those that contain an a:

$(function() { // or $(document).ready(function() {
    $(".product").each(function() {
        if ($(this).has('a')) {
            $(this).css("cursor", "pointer");
            $(this).click(
            function () {
                window.location = $(this).find('a').attr("href");
                return false;
            });
        }
    });
});
R. Bemrose
I guess it will add pointer to all .product blocks? I could do that via css too, but only the linked ones should have that.
Nimbuz
@Nimbuz: OK... let me play around with this for a moment...
R. Bemrose
@Nimbuz: This edited version may be more along the lines of what you want.
R. Bemrose
$(".product:has(a[href])") should give you products with links
Vincent Robert
+4  A: 
  1. Find all the products that have a link
  2. Puts a pointer as cursor
  3. Handle the click event to change location

The code:

$(".product:has(a[href])")
    .css("cursor", "pointer")
    .click(function()
    {
        window.location = $("a", this).attr("href");
    });
Vincent Robert
Ah, I forgot that there's a :has selector in addition to a .has function. Whoops. (It doesn't help that the jQuery site search returns nothing for `has`)
R. Bemrose
Perfect! Thanks! :)
Nimbuz