7
votes

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

2
That assumes he's using jQuery. Maybe he is, or maybe he's just confused because he saw .val() in some other questions. - Barmar
@Barmar ya I realized that as the OP just kept javascript tag. - Mr_Green

2 Answers

24
votes

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

1
votes

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}`)
}