Reputation: 43
I'm looking for a method in which I can replace an established link by using jQuery
<ul class="navigation"
<li class="level0 nav-1 level-top first parent">
<a href="mylink.com/whats-new.html" class="level-top"><span>What's New</span></a>
</li>
...
Ideally I'd like to replace the link with javascript:void(0) without affecting any other items.
Some expert help would be really appreciated.
Thank you very much
PS: I've tried
<script type="text/javascript">
$(document).ready(function() {
$('a').attr('href', 'javascript:void(0)');
});
</script>
earlier. This works but affects all links.
Upvotes: 0
Views: 98
Reputation: 2185
You can do like this
$(document).ready(function(){
$('ul.navigation a').click(function(event){
event.preventDefault();
$(this).attr("href","javascript:void(0)");
});
});
Upvotes: 0
Reputation: 803
If you want to replace the url of a specific anchor tag then you have to provide the relevant selector of that element.
jQuery code
$(".navigation li.first a.level-top").attr('href', 'javascript:void(0)');
or
$("a[href=mylink.com/whats-new.html]").attr('href', 'javascript:void(0)');
Upvotes: 0
Reputation: 59292
According to your comment,
You can just do this by using selector .nav-1 a
, which targets <a>
which are descedents of .nav-1
$('.nav-1 a').attr('href', 'javascript:void(0)');
But, I would suggest you to prevent the default action
$('.nav-1 a').click(function(e){
e.preventDefault();
});
Upvotes: 2