Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On click get button Value

Tags:

javascript

I have a button like following

<input type='button' value='Generate' onclick='f1()' />

now the f1 function should show a alert box contain button value. in this case 'Generate'

How to do this?

I tried

alert(this);
alert(this.val());

it does not work

like image 878
LynAs Avatar asked May 26 '13 03:05

LynAs


2 Answers

Try this.

<input type='button' value='Generate' onclick='f1(this)' />

Now alert like

function f1(objButton){  
    alert(objButton.value);
}

P.S: val() is actually a jQuery implementation of value

like image 105
naveen Avatar answered Oct 11 '22 18:10

naveen


Or you do this:

<button id='button' value='Generate' onclick='f1()'>Generate</button>

Then this for javascript:

Const click = document.getElementById('button')
Function f1{
  Alert(`${click.Value}`)
}
like image 24
amaugo somto Avatar answered Oct 11 '22 19:10

amaugo somto