1

To the point, if i input value "20" in input field then show message "Thank you".

Here's my HTML Code:

<form method="post" action="">
<input type="text" id="nominal" value="">
<input type="submit" value="submit">
</form>

Here's my JS Code:

$(document).ready(function(){

    var money = 20;

    /* not sure if this is written correctly, but this is supposed to
    check whether the hidden input element value is equal to var money */

    if ($("input[id='nominal']").val() == money )  {

     var h = document.createElement("H1")                // Create a <h1> element
     var t = document.createTextNode("Thank You");     // Create a text node
     h.appendChild(t);                                   // Append the text to <h1>

    };
});

i've created one script to fulfill what I need, but not working! what's wrong?

My JDFIDDLE LINK

3
  • Put you code in $("#nominal").change(function(){}) Commented Jul 19, 2016 at 9:47
  • Add $('body').append(h); after h.appendChild(t); Commented Jul 19, 2016 at 9:47
  • @ThinkDifferent not working Commented Jul 19, 2016 at 9:59

2 Answers 2

2

You have to create an event to listening for changes, in this case changed. And you can make your code a bit smaller too. ;)

$(function() {
    $("#nominal").change(function() {
        if( $(this).val() == 20 )
            $(this).after("<h1>Thank You</h1>");
    });
});

Full working exaple with removing the message when value changes again and strict check can be seen here.

Sign up to request clarification or add additional context in comments.

2 Comments

Sure it does. For change you have to leave focus. If you not want to, use keyup too. See here, I created a demo. And a check to prevent double show message: jsfiddle.net/fma1hyoL/1
This will remove the message too: jsfiddle.net/fma1hyoL/4 Hope this is what you want. Not that I cast the money into a string too, because otherwise when the user added spaces, like 20 it will show a message too. So it is more strict ...
2

$(document).ready(function(){
    var money = 20;
    $("#nominal").change(function() { // take change event of input box
       if ($(this).val() == money )  { // use $(this) to take value
          var h = document.createElement("H1"); // Create a <h1> element
          var t = document.createTextNode("Thank You"); // Create a text node
          h.appendChild(t);      
          $('form').append(h); // append created h1 element in form/html
       } else {
          $('form').find("h1").remove();
       }
     })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<form method="post" action="">
<input type="text" id="nominal" value="">
<input type="button" value="submit" name="submit" id="submit">
</form>

1 Comment

it's work, but when i delete value, the text still showing

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.