javascript - How to get button value in jquery - Stack Overflow

Unable to get the button value in jquery.<head><scriptsrc=".1.0jquery.min.js&qu

Unable to get the button value in jquery.

<head>
    <script        
src=".1.0/jquery.min.js">  
</script>
</head>

<body>
    <p> This is para </p>
    <button> Hide </button>
    <script>
            function handle(){
                    value = $("button").attr("value");
                    alert(value);
            }

            $(function(){
                    $("button").click(handle);
            });
    </script>

What is the error on the above code. I need to handle the event triggered by the button, instead of "this". I already saw how to solve it using "this". So without "this", how to handle event

Unable to get the button value in jquery.

<head>
    <script        
src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js">  
</script>
</head>

<body>
    <p> This is para </p>
    <button> Hide </button>
    <script>
            function handle(){
                    value = $("button").attr("value");
                    alert(value);
            }

            $(function(){
                    $("button").click(handle);
            });
    </script>

What is the error on the above code. I need to handle the event triggered by the button, instead of "this". I already saw how to solve it using "this". So without "this", how to handle event

Share Improve this question asked Sep 27, 2016 at 19:48 mohangrajmohangraj 11.2k19 gold badges64 silver badges98 bronze badges 1
  • 2 The button has no value attribute... – J. Titus Commented Sep 27, 2016 at 19:49
Add a ment  | 

3 Answers 3

Reset to default 2

If you want to get the Text inside your button just use:

value = $("button").text() instead of value = $("button").attr("value")

Your button does not have the value attribute from which you can get the data.

So you should use .text() method instead of .attr()

<body>

  <p> This is para </p>
  <button> Hide </button>

<script src="https://code.jquery./jquery-3.1.0.js"></script>

<script>
  function handle(){
    value = $("button").text();
    alert(value);
  }

  $(function(){
    $("button").click(handle);
  });
</script>

</body>

Working example in JSBin

Use the jQuery .text() method with the button element to grab the text value contained within. (e.g. The text between the open <button> and close </button> tags.)

What you're attempting is not working because the button element has no value attribute to retrieve.

Get Text Example:

<script src="https://ajax.googleapis./ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<p> This is para </p>

<button> Hide </button>

<script>
    function handle() {
        var buttonText = $("button").text();

        // Whatever you need to do with the text.
        alert(buttonText);
    }

    $(function(){
         $("button").click(handle);
    });
</script>

Working code pen

Conversely, you can also set the button's text value using the same method by passing the desired value to it.

Set Text Example:

...
    $("button").text("My New Text");
...

Documentation: jQuery .text() Method

button element has no value attribute associated so you must try grabbing the text within the tag <button></button>

you can use jquery's text() method to grab the text as

<html>
<head><title>demo</title>
<script src="https://code.jquery./jquery-3.1.0.js"></script>
<script type='text/javascript'>
      $(document).ready(function(){
      var x= $('#btn').text();
      console.log(x);
});
</script>
</head>
<body>
<button id='btn'>Click Me</button>
</body>
</html>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744070580a4553464.html

相关推荐

  • javascript - How to get button value in jquery - Stack Overflow

    Unable to get the button value in jquery.<head><scriptsrc=".1.0jquery.min.js&qu

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信