user3396187
user3396187

Reputation: 63

Display value of input onto alert box using jquery

Good day, I just want to know how do you print out the value you entered on the text field via alert box, I'm new to jquery and I don't have any idea what am I doing. Any help would be appreciated. Thank you

             <form action="" align="center" method="post">
             Name:<br>
             <input type="text" name="fullname" required>
             <input type="submit" onclick="printOut();">
             </form>


             $(document).ready(function(){
             $('form').submit(function(e){
             e.printOut();


            alert ("Hi "+$("input[name="fullname"]").val()+");

Upvotes: 0

Views: 3080

Answers (3)

Dzanan Begovic
Dzanan Begovic

Reputation: 160

Try something like this:

 <form action="" align="center" method="post">
                 Name:<br>
                 <input type="text" name="fullname" required>
                 <input type="submit"">
 </form>

<script>
    $('form').submit(function(e){
        e.preventDefault();
        alert("Hi "+ $("input[name='fullname']").val());
    });

</script>

Upvotes: 0

Matthew Schlachter
Matthew Schlachter

Reputation: 3350

The error is in your JQuery selector, the unescaped quotes inside of your string is breaking the syntax. Try this:

$(document).ready(function(){
    $('form').submit(function(e){
        alert ("Hi "+$("input[name='fullname']").val());
        return false;
    });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form align="center">
    Name:<br />
    <input type="text" name="fullname" required />
    <input type="submit" />
</form>

Upvotes: 1

QaMar ALi
QaMar ALi

Reputation: 237

you are using the wrong css $("input[name="fullname"]")selector everything else is okay it should be $("input[name=fullname]")

try this code

 <form action="" align="center" method="post">
         Name:<br>
         <input type="text" name="fullname" required>
         <input type="submit" onclick="printOut();">
  </form>

  <script>
         $(document).ready(function(){
         $('form').submit(function(e){
          e.preventDefault();

        alert ("Hi "+$("input[name="fullname"]").val()+");
 </script>

Upvotes: 0

Related Questions