最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Changing button text by Click in JavaScript - Stack Overflow

programmeradmin0浏览0评论

I'm using following codes in a JSP page.This is my function to change button text by a button click in JavaScript.

    <script>
    function click(){
        var el=document.getElementById(btnaccept);
        if(el.value=="Accept"){
           el.value==="Accepted"; 
        }else{
            el.value==="Accept";
        }
    } 
 </script>

This is my button;

<button type="button" class="btn btn-default btn-sm" id="btnaccept"  onclick="click()">
                <span class="glyphicon glyphicon-ok" aria-hidden="true" name="accept"></span> Accept
      </button>

This code is not functioning when I click the "Accept" button. What error I have done here?

I'm using following codes in a JSP page.This is my function to change button text by a button click in JavaScript.

    <script>
    function click(){
        var el=document.getElementById(btnaccept);
        if(el.value=="Accept"){
           el.value==="Accepted"; 
        }else{
            el.value==="Accept";
        }
    } 
 </script>

This is my button;

<button type="button" class="btn btn-default btn-sm" id="btnaccept"  onclick="click()">
                <span class="glyphicon glyphicon-ok" aria-hidden="true" name="accept"></span> Accept
      </button>

This code is not functioning when I click the "Accept" button. What error I have done here?

Share Improve this question asked Aug 29, 2015 at 5:06 hinatahinata 3851 gold badge4 silver badges12 bronze badges 1
  • 1 === does a parison. You want =, which assigns a new value. – mattt Commented Aug 29, 2015 at 5:08
Add a ment  | 

5 Answers 5

Reset to default 3

Try this code

<script>
      function click1() {
        var el = document.getElementById('btnaccept');
        //alert(el.innerHTML);
        if (el.textContent == "Accept") {
          el.textContent = "Accepted";
        } else {
          el.textContent = "Accept";
        }
      }
    </script>

change onclick event to click1()

A few things:

  • Don't name your funtion click that won't work.
  • You are not using value on the button, you are using inner HTML.
  • use if you want to manipulate the value, or manipulate ineerHTML if you want the other way around.
  • document.getElementById takes string as argument, so you want to write there document.getElementById('btnaccept');
  • Use = to assign value === is parison (equal value or type)

See working demo

First thing you should use click as function name use any other name. It will treat it as mouseevent docs

=== is used for parison to assign value use =.

Here I have used clickbutton() instead of click() for the function name.

HTML

<button type="button" class="btn btn-default btn-sm" id="btnaccept"  onclick="clickbutton()">

Script

function clickbutton() {
   var el = document.getElementById('btnaccept');
   if (el.value == "Accept") {
     el.value = "Accepted";
   } else {
     el.value = "Accept";
   }
 }

Demo here

As mattt said, you need to use the assignment operator = instead of the equality operator === when assigning values. Also, when using getElementById, it will be expecting a string, so you want to use getElementById("btnaccept");

As well, to set the text of a button element I believe you will need to use el.innerText = "Accept";

I implemented a toggle button for a colapsible panel in the following way:

<button type="button" class="btn btn-default" onclick="changeIcon(this)">
<span class="glyphicon glyphicon-triangle-bottom"></span>
</button>

<script>
function changeIcon(button) {

    if(button.firstElementChild.getAttribute("class") == "glyphicon glyphicon-triangle-bottom")
    {
        button.firstElementChild.setAttribute("class", "glyphicon glyphicon-triangle-top");
    }
    else
    {
        button.firstElementChild.setAttribute("class", "glyphicon glyphicon-triangle-bottom");
    }
}
</script>
发布评论

评论列表(0)

  1. 暂无评论