1

I have a thumbnail list:

<ul>
  <li><a href=#c1><img /><p>1 Thumb</p></a></li>
  <li><a href=#c2><img /><p>2. Thumb</p></a></li>
  <li><a href=#c3><img /><p>3. Thumb</p></a></li>
</ul>

And javascript (i use jquery framework) should change every href of the a's in the ul to javascript:void(0)

It's like:

$("#thumbs ul li > a").href( 'javascript:void(0)');
caitriona
  • 8,569
  • 4
  • 32
  • 36
Tomkay
  • 5,120
  • 21
  • 60
  • 92

1 Answers1

4

You can do what you want using .attr() (used for attributes, instead of .href()) like this:

$("#thumbs ul li > a").attr('href','javascript:void(0)');

...but I wouldn't, there's a better way to solve your problem, for example:

$("#thumbs ul li > a").click(function(e) {
  e.preventDefault();
});

This attaches a property click handler to prevent the navigation, rather than messing with attributes to do the same.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • 1., It's cleaner, 2., It's putting middle man between click and action, rather than modifying DOM. – Adam Kiss Dec 16 '10 at 10:40
  • @Sandeepan - for a couple of reasons, check out this question for a lengthier discussion: http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0 But there are other disadvantages as well, ever changing the `href` back, or using it for anything later for example...all prevented if you blow it away, when you can do the same action leaving it intact. – Nick Craver Dec 16 '10 at 10:41