Changing button text onclick

后端 未结 18 2410
野性不改
野性不改 2020-11-27 03:10

When I click on myButton1 button, I want the value to change to Close Curtain from Open Curtain.
HTML:

<         


        
相关标签:
18条回答
  • 2020-11-27 03:54

    When using the <button> element (or maybe others?) setting 'value' will not change the text, but innerHTML will.

    var btn = document.getElementById("mybtn");
    btn.value = 'my value'; // will just add a hidden value
    btn.innerHTML = 'my text';
    

    When printed to the console:

    <button id="mybtn" class="btn btn-primary" onclick="confirm()" value="my value">my text</button>

    0 讨论(0)
  • 2020-11-27 03:56
    This is simple way to change Submit to loading state   
    
     <button id="custSub" type="submit" class="button left tiny" data-text-swap="Processing.. &nbsp;">Submit &nbsp;<i class="fa fa-angle-double-right"></i></button>
    
    
        <script>
        jQuery(document).ready(function ($) {
    
            $("button").on("click", function() {
              var el = $(this);
              if (el.html() == el.data("text-swap")) {
                el.html(el.data("text-original"));
              } else {
                el.data("text-original", el.html());
                el.html(el.data("text-swap"));
              }
              setTimeout(function () {
                    el.html(el.data("text-original"));
               }, 500);
            });
    
        });
        </script>
    
    0 讨论(0)
  • 2020-11-27 03:57

    You are missing an opening quote on the id= and you have a semi-colon after the function declaration. Also, the input tag does not need a closing tag.

    This works:

    <input onclick="change()" type="button" value="Open Curtain" id="myButton1">
    
    <script type="text/javascript">
    function change()
    {
    document.getElementById("myButton1").value="Close Curtain";
    }
    </script>
    
    0 讨论(0)
  • 2020-11-27 03:59

    Add this function to the script

    function myFunction() {
    
                    var btn = document.getElementById("myButton");
    
                    if (btn.value == "Open Curtain") {
                        btn.value = "Close Curtain";
                        btn.innerHTML = "Close Curtain";
                    }
                    else {
                        btn.value = "Open Curtain";
                        btn.innerHTML = "Open Curtain";
                    }
    
                }
    

    and edit the button

    <button onclick="myFunction()" id="myButton" value="Open Curtain">Open Curtain</button>
    
    0 讨论(0)
  • 2020-11-27 04:00

    Or more simple without having to name the element (with 'button' element):

    <button onclick="toggleLog(this)">Stop logs</button>
    

    and script :

    var bWriteLog = true;
    
    function toggleLog(elt) {
    
      bWriteLog = !bWriteLog;
      elt.innerHTML = bWriteLog ? 'Stop logs' : 'Watch logs';
    }
    
    0 讨论(0)
  • 2020-11-27 04:02

    this can be done easily with a vbs code (as i'm not so familiar with js )

    <input type="button" id="btn" Value="Close" onclick="check">
    <script Language="VBScript">
    sub check
    if btn.Value="Close" then btn.Value="Open" 
    end sub
    </script>
    

    and you're done , however this changes the Name to display only and does not change the function {onclick} , i did some researches on how to do the second one and seem there isnt' something like

    btn.onclick = ".."
    

    but i figured out a way using <"span"> tag it goes like this :

    <script Language="VBScript">
      Sub function1
      MsgBox "function1"
      span.InnerHTML= "<Input type=""button"" Value=""button2"" onclick=""function2"">"
      End Sub
    
       Sub function2
      MsgBox "function2"
      span.InnerHTML = "<Input type=""button"" Value=""button1"" onclick=""function1"">"
      End Sub
      </script>
      <body>
      <span id="span" name="span" >
      <input type="button" Value="button1" onclick="function1">
      </span>
      </body>
    

    try it yourself , change the codes in sub function1 and sub function2, basically all you need to know to make it in jscript is the line

    span.InnerHTML = "..." 
    

    the rest is your code you wanna execute

    hope this helps :D

    0 讨论(0)
提交回复
热议问题