0

so I have something like this -

<a href="link..." class="text-box-a-href" >
<div class="text-box">
<h4>text...</h4>
<p> text....</p>
<span> date.</span>
</div>
</a>

What I need is, if someone clicks on link with class="text-box-a-href" it automatically adds class .visited to h4. Something like this -

  $('.text-box-a-href').click(function() {

    var clicked_element;

    clicked_element = $(this);

    $(clicked_element).children('h4').addClass('visited');

    });

But nothing happens. On homepage I got like 10000 elements with so you know. Thanks.

user980952
  • 35
  • 9

1 Answers1

0

I'm not 100% sure what you're trying to do. But the page will just reload using that script. The class will get added, then the page reloads and the DOM get's rebuilt.

$('.text-box-a-href').click(function()
{

    $(this).find('h4').addClass('visited');

    return false; // Without this the page will just reload?

});
Alex
  • 7,320
  • 1
  • 18
  • 31
  • Ah, the h4 is a child of the anchor as there is an extra div. Change the `children` to `find` as in my updated answer – Alex Oct 12 '11 at 10:52
  • It is recommended to use `preventDefault()` instead of `return: false` for cases like this. http://stackoverflow.com/questions/1357118/javascript-event-preventdefault-vs-return-false – mqchen Oct 12 '11 at 10:54