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?
Nick Craver
631k138 gold badges1.3k silver badges1.2k bronze badges
asked Dec 28, 2009 at 7:20
Steven
25.5k45 gold badges113 silver badges137 bronze badges
2 Answers 2
make sure you wrap your jQuery script in $(document).ready().
$(document).ready(function() {
// your jQuery script here.
});
OR
$(function(){
// your jQuery script here.
});
answered Dec 28, 2009 at 7:28
Anwar Chandra
8,6699 gold badges47 silver badges61 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Steven
It doesn't work even I wrap the Jquery script in $(document).ready().
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
answered Dec 28, 2009 at 7:41
Tinku
1,6182 gold badges16 silver badges27 bronze badges
1 Comment
Steven
Yes it exists in the page/DOM.
lang-js
$( "input[ id ^= 'question' ]" )