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()" />
Another solution could be using jquery button selector inside the if else statement $("#buttonId").text("your text");
If the
HTMLElement
isinput[type='button']
,input[type='submit']
, etc.change it using this code:
If, the
HTMLElement
isbutton[type='button']
,button[type='submit']
, etc:change it using any of these methods,
Please note that
input
is an empty tag and cannot haveinnerHTML
,innerText
ortextContent
button
is a container tag and can haveinnerHTML
,innerText
ortextContent
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.
And since you are running the button at
server
the ID may get mangled in the framework. I so, tryAnother better way to do this is like this.
On markup, change your onclick attribute like this.
onclick="showFilterItem(this)"
Now use it like this
innerText is the current correct answer for this. The other answers are outdated and incorrect.
innerHTML also works, and can be used to insert HTML.
I know this question has been answered but I also see there is another way missing which I would like to cover it.There are multiple ways to achieve this.
1- innerHTML
You can insert HTML into this. But the disadvantage of this method is, it has cross site security attacks. So for adding text, its better to avoid this for security reasons.
2- innerText
This will also achieve the result but its heavy under the hood as it requires some layout system information, due to which the performance decreases. Unlike innerHTML, you cannot insert the HTML tags with this. Check Performance Here
3- textContent
This will also achieve the same result but it doesn't have security issues like innerHTML as it doesn't parse HTML like innerText. Besides, it is also light due to which performance increases.
So if a text has to be added like above, then its better to use textContent.
You can toggle
filterstatus
value like thisSo your function looks like