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>
HelloCW
  • 843
  • 22
  • 125
  • 310

4 Answers4

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>
mohamedrias
  • 18,326
  • 2
  • 38
  • 47
  • Thanks! but when I move cursor hang to the text "Contact Us", the cursor still display the text as link – HelloCW Mar 31 '15 at 01:57
  • 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 – mohamedrias Mar 31 '15 at 01:58
  • I've added a demo as well to show how it works. Please check @HelloCW – mohamedrias Mar 31 '15 at 02:05
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');
Norlihazmey Ghazali
  • 9,000
  • 1
  • 23
  • 40
0

To remove the href you can use

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

To remove only the link you can use

$('#Main4').attr('href','');
abiieez
  • 3,139
  • 14
  • 57
  • 110
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.

Community
  • 1
  • 1
Todd Mark
  • 1,847
  • 13
  • 25