I have input fields that change to buttons when a word is correct. I however now want my timer to stop when there is no existing input field anymore. How would this be done? This is how I START the timer:
$(document).one('keydown', '.syl-input', function(){
T.start();
});
Now: I would like to create a check to see whether this input field still exists or not:
var sylInput = $('<input/>', {
'type': 'text',
'class': 'form-control syl-input',
'name': +c++,
'id': +idsyll++
})
if it doesn't exist anymore, it should stop the timer T.stop()
, but as long as there are input fields it should not fire. I did found something related to what I want, but it's not entirely what I want --> Is there an "exists" function for jQuery?.
I had the next thing in mind (based on the link above)
if ('.syl-input' === 0) {
T.stop();
}
but I assume there should be an each
loop before I can create this if
statement?
To show you the complete code where my input field is in (maybe that will be a gamechanger)?
$.map(exercise.syllables, function (syllable, j) {
if (!syllable || !syllable.trim().length) {
// If it doesn't exist or is an empty string, return early without creating/appending elements
return;
}
var innerSylCol = $('<div/>', {
class: 'col-md-3 inputSyllables'
});
var sylInput = $('<input/>', {
'type': 'text',
'class': 'form-control syl-input',
'name': +c++,
'id': +idsyll++
}).on('blur', function() {
var cValue = $(this).val();
if(cValue === "") {
return;
}
if (cValue === syllable) {
correctSylls.push(cValue);
console.log(correctSylls);
}
if (exercise.syllables.length === correctSylls.length) {
$(this).closest('.syll-row').find('input.syl-input').each(function () {
$(this).replaceWith(getCorrectBtn($(this).val()))
});
S.addRight();
S.playRight();
} else if (cValue !== syllable){
// $(this).css({'color':'#e00413'});
S.playWrong();
S.addWrong();
}
});
innerSylCol.append(sylInput);
sylRow.append(innerSylCol);
});
idsyll = 0;
sylCol.append(sylRow);
exer.append(colLeft, sylCol);
exerciseArea.append(exer);
});
return exerciseArea;
}