1

I hope to remove the link "ContactUs.aspx" of the following code, I use the code $("#Main4").contents().unwrap(); it worked, but the class "LeftMainMenu" is removed also.

I hope to remove only the link, how can I do this?

<a id="Main4" class="LeftMainMenu" href="ContactUs.aspx">Contact Us</a>

4 Answers 4

2
<a id="Main4" class="LeftMainMenu" href="ContactUs.aspx">Contact Us</a>

This will remove the href attribute:

$("#Main4").removeAttr("href")

Remember to do it on page load event like below:

$(function() {
    $("#Main4").removeAttr("href")
});

Or if you just want to remove the value of href then

   $("#Main4").attr("href", "")

This will make

<a id="Main4" class="LeftMainMenu" href="">Contact Us</a>

DEMO:

$(function() {
   $("#Main4").removeAttr("href")
});
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.0.js"></script>

<a id="Main4" class="LeftMainMenu" href="ContactUs.aspx">Contact Us</a>

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! but when I move cursor hang to the text "Contact Us", the cursor still display the text as link
You mus do it on page load. Like $(function(){ $("#Main4").removeAttr("href"); }); Looks like when you're trying to remove the link was not added to the page. That's why it's not removed
I've added a demo as well to show how it works. Please check @HelloCW
0

Your jQuery selector is wrong. Should be $("#Main4"). You can use .removeAttr() to remove the attribute of href. See the documentation here.

$("#Main4").removeAttr('href');

Comments

0

To remove the href you can use

$('#Main4').removeAttr('href');

To remove only the link you can use

$('#Main4').attr('href','');

Comments

0

if you want the link hidden, you can:

$('#main4').hide();

if you want the link disabled, you can:

$('#main4').removeAttr('href');

or

$('#main4').attr('href','#');

or

$('#main4').attr('href','javascript:void(0);');

By the way, the different href attribute about 'javascript:void(0)' and '#' is there.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.