0

This is just simple code but not working..

why not working...?? I'm confuse..

<html> <head> <h3> Hello Web</h3> <br> </head> <body> <input type="text" name="textvalue" id="textvalue" /> <input type="submit" name="submit" value="SAVE" /> </body> </html> 

<--! jquery -->

function hello(){ $var=$('#textvalue').val(); alert($var); } 
3
  • why not working...?? Please elaborate the details on the issue you are facing. Commented Feb 1, 2018 at 4:36
  • What you are trying to do? and when it is not working? Commented Feb 1, 2018 at 4:40
  • ok.. i want on click submit button , get text type value. Commented Feb 1, 2018 at 4:46

5 Answers 5

2

Your html should contain a button

<input type='button' id='btnSubmit' value='submit'> 

it jquery should be like this

$("#btnSubmit").on('click',function(e){ e.preventDefault(); hello(); } 
Sign up to request clarification or add additional context in comments.

6 Comments

$var in the question could be a global variable. Also, OP has not specified what is the issue.
@Kumar_Vikas in this context it does not look like a global variable, Maybe he has not imported jquery properly.
oh! ok.. but. not working... i want button click and alert in get text data.
@koboso You may have to edit the question because what you are expecting isn't clear.
could you add <form> tag??
|
0

change var value = $('#textvalue').val(); alert(value);

2 Comments

$var in the question could be a global variable. Also, OP has not specified what is the issue.
i think it is not a global variable or maybe he forgot "cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"
0

The script must be connected at the end of the body.

<body> <input type="text" name="textvalue" id="textvalue" /> <input type="submit" name="submit" value="SAVE" /> <script src="js.js" <body> 

And where do you call the function hello()?

1 Comment

@koboso if this post helped you might think accepting this as an answer.
0
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

Place the above line of code in head and than try. it will work..

<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> function hello(){ $var=$('#textvalue').val(); alert($var); } </script> </head> <body> <input type="text" name="textvalue" id="textvalue" /> <input type="submit" name="submit" onclick="hello()" value="SAVE" /> </body> </html> 

Comments

0

The reason that your code was not working is you did not call you function hello at any event.

You should not write as below

<head> <h3> Hello Web</h3> <br> </head> 

It should be like

<!DOCTYPE html> <html> <head> <title>Hello World</title> </head> <body> <form id="test-form" action="someurl" method="POST"> <input type="text" name="textvalue" id="textvalue" /> <input type="submit" name="submit" value="SAVE" /> </form> </body> </html> 

Below is the solution for your problem.

jQuery(document).ready(function($) { $('#test-form').submit(function(event) { // Prevent form submission event.preventDefault(); alert("Form submission prevented"); var textValue = $('#textvalue').val(); // Alert the value alert("textValue = " + textValue); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form id="test-form" action="someurl" method="POST"> <input type="text" name="textvalue" id="textvalue" /> <input type="submit" name="submit" value="SAVE" /> </form>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.