How to change the buttons text using javascript

后端 未结 5 1296
孤街浪徒
孤街浪徒 2021-01-30 10:10

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 showFilter         


        
5条回答
  •  隐瞒了意图╮
    2021-01-30 10:33

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

    
    
    

    change it using this code:

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

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

    
    
    

    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";
        }
    }
    

提交回复
热议问题