0

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!

5 Answers 5

2

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> 
Sign up to request clarification or add additional context in comments.

Comments

0
<input type="text" id="testTextarea">
<input type="submit" value="Test" onClick="alert(document.getElementById('testTextarea').value);" />

Comments

0
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>

Comments

0

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>  

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.