0

HTML code: The HTML code is dynamically created.

<li>
    <div class="above">What do I like best?</div>
    <div class="below">
    <label>Answer:(1 words)</label>
    <input id="question6" type="text" size="5"/>
    <label id="sign6"/>
    </div>
    </li>
    <li>
    <div class="above">What city do I like?</div>
    <div class="below">
    <label>Answer:(1 words)</label>
    <input id="question7" type="text" size="5"/>
    <label id="sign7"/>
    </div>

Jquery code:

function subjectivecheck(id){
        alert(id);
        var cost=(new Date().getTime() - start.getTime())/1000;
        var value=$('#question'+id).val();
        $.post("subjectivecheck.php?",{val:value, qid:id,time:cost, a_id:"<?php echo $announcementid; ?>"},function(xm){

            switch(parseInt(xm)){
                case 4:
                { $htm='Congrats,you have passed the test.';
                    $('#success').css({"color":"green"});
                    $('#success').text($htm);
                return; 
                }
                case 1:
                {
                $htm='V';
            $('#sign'+id).css({"color":"green"});
            $('#sign'+id).text($htm);
            break;  
                }
                case 0:{

                     $htm='X';
            $('#sign'+id).css({"color":"red"});
            $('#sign'+id).text($htm);
            break;
                }
                case 3:{
                    $('#subjectivequestion').text('You have failed at this announcement.');

                    $('#choicequestions').text(" ");
                }
            }

        });

    }
    var ajaxCallTimeoutID = null;
    $('input[id^=question]').keyup(function(ev){
        alert(this.id.substr(8));
    if (ajaxCallTimeoutID != null)
        clearTimeout(ajaxCallTimeoutID);

      ajaxCallTimeoutID = setTimeout(subjectivecheck(id), 1000);

    });

When I input something in question6 or

question7

, the function $('input[id^=question]').keyup(function(ev) },does not work, there is no alert() .Other jquery functions of this HTML file works fine. Any idea?

1
  • Try $( "input[ id ^= 'question' ]" ) Commented Dec 28, 2009 at 7:23

2 Answers 2

2

make sure you wrap your jQuery script in $(document).ready().

$(document).ready(function() {
  // your jQuery script here.
});

OR

$(function(){
  // your jQuery script here.
});
Sign up to request clarification or add additional context in comments.

1 Comment

It doesn't work even I wrap the Jquery script in $(document).ready().
0

Be sure that when you are binding that element "question7" or "question6" to keyup event , It exists in the page/DOM as you are generating it dynamically. You can check it in firebug

1 Comment

Yes it exists in the page/DOM.

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.