Reputation: 39
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>
Upvotes: 2
Views: 877
Reputation: 5122
Try this ...
<button onclick='alert_phrase("phrase")'>Alert this sentence</button>
Note: passing the id
as a string to alert_phrase.
Snippet:
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>
Upvotes: 3
Reputation: 4953
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>
Upvotes: 6