Tomkay
Tomkay

Reputation: 5160

JQuery - Change the href of the li a 's to javascript:void(0)

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)');

Upvotes: 1

Views: 7259

Answers (1)

Nick Craver
Nick Craver

Reputation: 630607

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.

Upvotes: 4

Related Questions