2016-11-26 1 views
2

Die onclick im folgenden Code löst keine Warnung aus.Onclick alert a string

Hier ist mein 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>

Antwort

3

Try this ...

<button onclick='alert_phrase("phrase")'>Alert this sentence</button> 

Hinweis: Um die id als String übergeben 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>

+0

Dank hilft! Es funktionierte @ rfornal – willy

+0

Ich bin froh, dass ich helfen konnte. Ich finde es hart, wenn ich diese selbst vermisse ... öfter als ich es möchte. – rfornal

6

Sie die Zitate auf Ihrem Parameter vergessen. Hope this

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>

Verwandte Themen