Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the buttons text using javascript

I have the following code to set the text of the button through javascript code , but it does not work it remains same the text remains same.

function showFilterItem() {     if (filterstatus == 0) {         filterstatus = 1;         $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().showFilterItem();         document.getElementById("ShowButton").innerHTML = "Hide Filter";     }     else {         filterstatus = 0;         $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().hideFilterItem();         document.getElementById("ShowButton").innerHTML = "Show filter";     } } 

And my html button code is

  <input  class="button black" id="ShowButton" type="button" runat="server"  value="Show Filter" onclick="showFilterItem()" /> 
like image 790
mahesh Avatar asked Aug 12 '11 05:08

mahesh


People also ask

How do I change the text style on a button in HTML?

How to Change Font Type in HTML. To change font type purely with HTML, use the CSS font-family property. Set it to the value you want and place it inside a style attribute. Then add this style attribute to an HTML element, like a paragraph, heading, button, or span tag.

How do I change the text of a button in CSS?

To change the font size of a button, use the font-size property.

How do you name a button in JavaScript?

The name property sets or returns the value of the name attribute of a button. The name attribute specifies the name of a button, and is used to reference form data after it has been submitted, or to reference the element in a JavaScript.


2 Answers

If the HTMLElement is input[type='button'], input[type='submit'], etc.

<input id="ShowButton" type="button" value="Show"> <input id="ShowButton" type="submit" value="Show"> 

change it using this code:

document.querySelector('#ShowButton').value = 'Hide'; 

If, the HTMLElement is button[type='button'], button[type='submit'], etc:

<button id="ShowButton" type="button">Show</button> <button id="ShowButton" type="submit">Show</button> 

change it using any of these methods,

document.querySelector('#ShowButton').innerHTML = 'Hide'; document.querySelector('#ShowButton').innerText = 'Hide'; document.querySelector('#ShowButton').textContent = 'Hide'; 

Please note that

  • input is an empty tag and cannot have innerHTML, innerText or textContent
  • button is a container tag and can have innerHTML, innerText or textContent

Ignore this answer if you ain't using asp.net-web-forms, asp.net-ajax and rad-grid

You must use value instead of innerHTML.

Try this.

document.getElementById("ShowButton").value= "Hide Filter"; 

And since you are running the button at server the ID may get mangled in the framework. I so, try

document.getElementById('<%=ShowButton.ClientID %>').value= "Hide Filter"; 

Another better way to do this is like this.

On markup, change your onclick attribute like this. onclick="showFilterItem(this)"

Now use it like this

function showFilterItem(objButton) {     if (filterstatus == 0) {         filterstatus = 1;         $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().showFilterItem();         objButton.value = "Hide Filter";     }     else {         filterstatus = 0;         $find('<%=FileAdminRadGrid.ClientID %>').get_masterTableView().hideFilterItem();         objButton.value = "Show filter";     } } 
like image 74
naveen Avatar answered Sep 28 '22 01:09

naveen


innerText is the current correct answer for this. The other answers are outdated and incorrect.

document.getElementById('ShowButton').innerText = 'Show filter'; 

innerHTML also works, and can be used to insert HTML.

like image 41
Maitreya Avatar answered Sep 28 '22 03:09

Maitreya