开发者

How do I change button Text on Click MVC?

开发者 https://www.devze.com 2022-12-20 18:43 出处:网络
I have a toggle button, with a show/hide feature in javascript.How do I change the text when the button is clicked?

I have a toggle button, with a show/hide feature in javascript. How do I change the text when the button is clicked?

<button id="Sho开发者_Python百科wHide">Show</button>


Using jQuery:

$('#ShowHide').toggle(
    function() { 
        $(this).text("Show");
        $('whatever').show();
    },
    function() { 
        $(this).text("Hide");
        $('whatever').hide();
    }
);


You can use this jquery to do the change.

$("#buttonName").val("New text for button"); 

JQuery is automatically included in your ASP.NET MVC Project.


You'll have to use Javascript. Either hard code the new value in the Javascript or use AJAX to call back to the server and get the new value.

If you're just going to hard code the value in your Javascript...jQuery makes it a snap:

('#yourButton').click(function(){
    (this).text('Hello click!');
});


In pure HTML/JS:

<input type="button" id="show_hide" onclick="this.value=(this.value=='Show')?('Hide'):('Show')" value="Show" />


$('#HideShow').click(function()
{
  if ($(this).text() == "Show")
  {
     $(this).text("Hide");
  }
  else
  {
     $(this).text("Show");
  };
});

EDIT: Updated to use the new valid ID which should be faster :) Brain cramp: cannot remember if it is .val() or .text() on the "button" tag as opposed to the <input type="button"> tag, no time to test atm but you get the idea (and I think I have it right:) ).

Alternative, the .toggle() has an .toggle(even,odd); functionality.

$('#HideShow').toggle(function() 
  { 
   $(this).text("Hide"); 
  }, 
  function() 
  { 
     $(this).text("Show"); 
  } 
); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号