Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2011
    Location
    Bellingham, WA
    Posts
    31
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Using jquery to check textboxes

    Hello,

    I'd like to write some jquery code which will check to see if the contents of a textbox are equal to some value after the user leaves the textbox. they should get a message saying Correct or Incorrect . As an example:

    Code:
     <td>-2</td>
        <td><input name="ans1" type="text" size="3" maxlength="3" /></td>
      </tr>  <  The correct answer here would be 5 >
      <tr>
        <td>-1</td>
        <td><input name="ans1" type="text" size="3" maxlength="3" /></td> < The correct answer here would be 4>
    And, I don't wish to abuse the forum, but as I'm learning javascript and jquery, I'd like to see solutions to both (I learn by example), so I'm posting the same question in the main JavaScript programming forum with a request for a Javascript solution; I hope that this isn't bad form!

    Thank you.....

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by kreut View Post
    I'd like to write some jquery code which will check to see if the contents of a textbox are equal to some value after the user leaves the textbox. they should get a message saying Correct or Incorrect
    Code:
    $('input[type=text]').blur(function(){
    	$('<span>').text(($(this).val()=='the_right_answer') ? 'Correct' : 'Wrongamundo').insertAfter($(this));
    });
    Quote Originally Posted by kreut View Post
    And, I don't wish to abuse the forum, but as I'm learning javascript and jquery, I'd like to see solutions to both (I learn by example), so I'm posting the same question in the main JavaScript programming forum with a request for a Javascript solution; I hope that this isn't bad form!.
    Cross-posting is generally frowned upon. But as a word of advice - don't get caught thinking you can learn jQuery instead of javascript. jQuery is a javascript framework; it's great at reducing the amount of code you have to write, but over-reliance on it will get you into all sorts of pain.

  • Users who have thanked Spudhead for this post:

    kreut (02-25-2011)

  • #3
    New Coder
    Join Date
    Feb 2011
    Location
    Bellingham, WA
    Posts
    31
    Thanks
    11
    Thanked 0 Times in 0 Posts
    Thank you for both your solution (it actually makes sense!) and your suggestion about understanding javascript (I got the same advice in the other forum).

    In addition, I won't do any cross postings in the future.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •