0

I have this simeple jQuery code:

$('#category_name').change(function() 
{
  alert('Handler for .change() called.');
  var category = $("#category_name").val();    
});

And this form:

    <form id="problem_categories" name="problem_category_form" method="post">
         <p>
         Category Name: <input type="text" size="50" name="category_name" id="category_name"></input>
         </p>                
         <p>
            <input type="hidden" id="category_problem_id" name="category_problem_id" value="<?php echo $problem_id; ?>" />

            <span class="error"   id="category_error" style="display:none"> Please Enter Valid Data</span>
            <span class="success" id="category_success" style="display:none"> Category Added Successfully!</span>

            <input type="submit" class="button" value="Add Category"></input>
         </p>
    </form>   

But for some reason the jQuery does not fire when I enter text into the form field. Here is an example page where this can happen on the bottom right: http://www.problemio.com/problems/problem.php?problem_id=51

3
  • hmmm...lol really? :) I think it fires on submit. But not on change. I was thinking it was going to fire when the person types. So I can help them find the item they are looking for by looking up what they wrote at that point of the form. Commented Oct 17, 2011 at 21:55
  • maybe you need keyup event.. change only fires once you leave the element Commented Oct 17, 2011 at 21:57
  • Seems to work for me - note the change event only fires after the element loses focus Commented Oct 17, 2011 at 22:01

3 Answers 3

2

Use keyup instead:

$('#category_name').keyup(function() 
{
  alert('Handler for .change() called.');
  var category = $("#category_name").val();    
});
Sign up to request clarification or add additional context in comments.

Comments

2

Your code is working, the change event won't fire until the textbox has lost focus.

You could try the 'keyup' event instead.

Comments

1

How about this one?

$(function() {
    cat_name = document.getElementById('category_name');
    $(cat_name).bind({
        keyup : function() {
            alert('Handler for .change() called.');
            var category = $("#category_name").val();
        }
    });
});

2 Comments

Why the document.getElementById
When you use ID instead of class its more safe to use document.getElementById. Specially if you write your script in head tag.

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.