简体   繁体   中英

alerting textbox through javascript

very simple question! i was wondering how to use javascript to alert something in my textbox...not sure what i'm doing now is not working. heres the function

function alertNow(comment){
alert(comment);
}

now here is the textbox

<input type = 'text' name = 'txt_comment'>
<button onClick = alertNow(txt_comment.value) value = "Submit Comment"></button> 

sure this is doable, i did it before but i just forgot some syntax prob. any ideas? thanks!

You are missing quotations. Also use the ID instead of name.

<input type = 'text' name = 'txt_comment' id='txt_comment'>
<button onClick = 'alertNow(txt_comment.value)' value = "Submit Comment"></button> 

Also better to use, document.getElementById like below

 <button onClick = 'alertNow(document.getElementById("txt_comment").value)' value = "Submit Comment"></button> 
<input type="text" id="testTextarea">
<input type="submit" value="Test" onClick="alert(document.getElementById('testTextarea').value);" />
function alertNow() {
 var a = document.getElementById("txt_comment").value;
 alert (a);
}

<input type = 'text' name = 'txt_comment' id="txt_comment">
<button onClick = "alertNow()" value= "Submit Comment"></button>

here is the code to complete your task...

// js code ...

function alertNow(){
   alert(document.getElementById('txt_comment').value);
   return false;
}

// html code ....

<form name="form_1" id="form_1" method="post">
     <input type = 'text' id='txt_comment' name = 'txt_comment'>
     <input type="button" onClick = "return alertNow();" value = "Submit Comment"/> 
</form>  

Use the ID instead of name.

function alertText() { alert(document.getElementById('txt_comment').value); }

<input type = 'text' name = 'txt_comment' id="txt_comment"> 
<button onClick = "alertText()" value= "Submit Comment"></button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM