close

How to pass the button value into my onclick event function?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How to pass the button value into my onclick event function? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How to pass the button value into my onclick event function ?

  1. How to pass the button value into my onclick event function ?

    to pass the button value into my onclick event function You can pass the value to the function using this.value, where this points to the button
    <input type="button" value="mybutton1" onclick="dosomething(this.value)">
    And then access that value in the function
    function dosomething(val){ console.log(val); }

  2. How to pass the button value into my onclick event function ?

    to pass the button value into my onclick event function You can pass the value to the function using this.value, where this points to the button
    <input type="button" value="mybutton1" onclick="dosomething(this.value)">
    And then access that value in the function
    function dosomething(val){ console.log(val); }

Method 1

You can pass the value to the function using this.value, where this points to the button

<input type="button" value="mybutton1" onclick="dosomething(this.value)">

And then access that value in the function

function dosomething(val){
  console.log(val);
}

Method 2

You can pass the element into the function <input type="button" value="mybutton1" onclick="dosomething(this)">test by passing this. Then in the function you can access the value like this:

function dosomething(element) {
  console.log(element.value);
}

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment