Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick alert a string

Tags:

javascript

The onclick in the code below is not firing the alert correctly.

Here is my code:

function alert_phrase(id){
  var value = document.getElementById(id).value;
  alert(value);
}
<div id="exp" style="background-color: white;">
  <p id="content-exp" style="color: red;">HELLO WORLD!</p>
  <input name="phrase" Placheholder="Enter text here" id="phrase" />
  <button onclick='alert_phrase(phrase)'>Alert this sentence</button>
</div>
like image 824
willy Avatar asked Dec 01 '25 09:12

willy


1 Answers

You're forgetting the quotes on your parameter. Hope this helps

function alert_phrase(id){
         var value = document.getElementById(id).value;
         alert(value);
 }
<div id="exp" style="background-color: white;">
<p id="content-exp" style="color: red;">HELLO WORLD!</p>
<input name="phrase" Placheholder="Enter text here" id="phrase" />
<button onclick='alert_phrase("phrase")'>Alert this sentence</button>
like image 146
HenryDev Avatar answered Dec 04 '25 07:12

HenryDev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!