user1211185
user1211185

Reputation: 731

How to change HTML link's href property dynamically

Hi I have a HTML link on my MVC3 View.

I want to change its href property each time user clicks it.

<a class="tabs" href="#educationDetails">
<input id="SubmitBtn" type="submit" value="Next" />
</a>

Is there any way to solve this issue.

Many Thanks

Upvotes: 2

Views: 11225

Answers (2)

Joberror
Joberror

Reputation: 5890

$("a.tabs").click(function() {
    this.href = 'newhref';
    return false;
});

It is more efficient this way compared to @ocanal solution.

Source:

http://net.tutsplus.com/tutorials/javascript-ajax/14-helpful-jquery-tricks-notes-and-best-practices/

Upvotes: 1

Okan Kocyigit
Okan Kocyigit

Reputation: 13421

$(".tabs").click(function() {
   $(this).attr("href","newhref.com");
});

UPDATE


you can get attribute value like this,

$(this).attr("href")  //will return '#educationDetails'

so you can check that value like this,

$(".tabs").click(function() {
  if ($(this).attr("href") == "#tab1")
      $(this).attr("href","#tab2");
  else if ($(this).attr("href") == "#tab2")
      $(this).attr("href","#tab1");
});

UPDATE-2


If you just want to change #tab1 to #tab2, not reverse. you can also do it like this way,

$('a.tabs[href="#tab1"]')​.click(function() {
    $(this).attr("href","#tab2");​
})​;​

Upvotes: 6

Related Questions