2

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

7
  • Use JS on the click event <input id="SubmitBtn" type="submit" value="Next" onclick="changeUrls();" /> Commented Apr 16, 2012 at 11:07
  • 2
    Does the link go to another page? Or is it an internal link? Because if it goes to an external page then you'll need a server-side solution to remember the number of clicks. Commented Apr 16, 2012 at 11:07
  • 1
    Why do you have a submit button within an a-tag? Commented Apr 16, 2012 at 11:10
  • 2
    Your HTML is invalid. <a> elements cannot contain interactive content. Commented Apr 16, 2012 at 11:26
  • 1
    @Quentin: Quite true. @ user: See the content model of the a element, which links to the definition of interactive content. This is unrelated to your question, but still a useful thing for Quentin to point out. Commented Apr 16, 2012 at 11:59

2 Answers 2

6
$(".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");​
})​;​
Sign up to request clarification or add additional context in comments.

5 Comments

can you please show me how I can use the switch condition to see it the current href value is this "#tab1" then set it to "tab2". Thanks in Advance.
@user1211185 If this is the correct answer to your question you should mark this as the answer
Hi Jayanga, I tried to mark it but it says I can mark it after 10 minutes may be I don't have enough reputations. thanks
It's your question, you can accept any answer to any of your own question(s) (which rewards you with a measure of rep). All you have to do is wait for the timer to clear and allow you to do so.
@ocanal no I have 7 tabs on my page and there is no previous button at the moment. I am just moving forward.
1
$("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/

3 Comments

If you're going to make that claim, might I recommend you pay a visit to JS Perf and provide support/evidence? (You're right, but...y'know, cite your sources. =))
"Please note that there are three attributes that should always be accessed, via jQuery: “src,” “href,” and “style.” These attributes require the use of getAttribute in older versions of IE."

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.