简体   繁体   中英

changing the button name when clicked

I have a bootstrap button which displays "Show User Information" as the button lable. When user click on the button, i want to change the label to "Display Only Users" and again when user click on "Display Only Users" it has to change the button label to "Show User Information".

 <button class="btn btn-primary" type="button" onclick="myFunction()" id="myButton1">Show User Information</button>

js code:

  function myFunction() {
        var elem = document.getElementById("myButton1");
         if (elem=="Show User Information") elem.value = "Display Only Users";
        else elem.value = "Show User Information";
}

The above js code is not changing the button label when clicked. Any inputs?

Add value attribute to the button ,

value="Show User Information"

and retrieve in js using and compare

ele.value=="Show User Information"

You need to check the text of the button, not the element itself

 function myFunction() { var elem = document.getElementById("myButton1"), text = elem.textContent || elem.innerText; if (text === "Show User Information") elem.innerHTML = "Display Only Users"; else elem.innerHTML = "Show User Information"; } 
 <button class="btn btn-primary" type="button" onclick="myFunction()" id="myButton1">Show User Information</button> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM