I use the following function on keyup event to redirect to another javascript function. Problem is it does'nt seem to fire, although it does bind the function to the textbox.
$(document).ready(function () {
EnablePickTargetButton();
//clear contents; use a delay because tinyMCE editor isn't always fully loaded on document.ready
var t = setTimeout(function () {
if (typeof textEditorForCreate != 'undefined' && tinymce.editors.length > 0)
tinyMCE.activeEditor.setContent('');
}, 300);
var txtSearchUser = $('#txtSearchUser');
if(typeof txtSearchUser != 'undefined')
{
$('#txtSearchUser').keyup(function (e) {
if (e.keyCode == 13) {
e.preventDefault();
searchUser();
}
else
alert('cucu');
});
}
});
Not even the alert shows up. Checking the html, I can see it doesn't add onkeyup to the textbox; The textbox is in a popup window hosted in a div on the form; But on document.ready it runs the function without error.
I use the following function on keyup event to redirect to another javascript function. Problem is it does'nt seem to fire, although it does bind the function to the textbox.
$(document).ready(function () {
EnablePickTargetButton();
//clear contents; use a delay because tinyMCE editor isn't always fully loaded on document.ready
var t = setTimeout(function () {
if (typeof textEditorForCreate != 'undefined' && tinymce.editors.length > 0)
tinyMCE.activeEditor.setContent('');
}, 300);
var txtSearchUser = $('#txtSearchUser');
if(typeof txtSearchUser != 'undefined')
{
$('#txtSearchUser').keyup(function (e) {
if (e.keyCode == 13) {
e.preventDefault();
searchUser();
}
else
alert('cucu');
});
}
});
Not even the alert shows up. Checking the html, I can see it doesn't add onkeyup to the textbox; The textbox is in a popup window hosted in a div on the form; But on document.ready it runs the function without error.
Share Improve this question edited Sep 6, 2013 at 13:48 Octavian Epure asked Sep 6, 2013 at 13:36 Octavian EpureOctavian Epure 1,0395 gold badges19 silver badges35 bronze badges 15- 3 Works fine on jsFiddle jsfiddle/e8JSA Have you added document.ready and included jQuery to the document? – Anton Commented Sep 6, 2013 at 13:39
- How do you know it has bound to the box if it does not fire the event? @Anton ment seems to be the answer though. – Rory McCrossan Commented Sep 6, 2013 at 13:40
- @insertusernamehere The URL is jsFiddle main page – PurkkaKoodari Commented Sep 6, 2013 at 13:40
- @Anton You beat me by 9 secs. :) – insertusernamehere Commented Sep 6, 2013 at 13:40
- Yes work fine for me too jsfiddle/charaf11/nLYbX you may need to add ready() or your jquery is not loaded correctly – Charaf JRA Commented Sep 6, 2013 at 13:41
3 Answers
Reset to default 5Try this delegate on document or closest static element. (If the element is added dynamically)
$(document).on('keyup','#txtSearchUser',function(){
//Code
});
it works :
$(document).ready(function(){
$('#txtSearchUser').keyup(function (e) {
if (e.keyCode == 13) {
e.preventDefault();
}
else
alert('cucu');
});
});
http://jsfiddle/jMk5S/
check if you're referencing your html element properly. Perhaps you mix id with the class?
I edited your code and is working :
The problem is in your document ready function , you have a syntax error , next time check console in your browser to see what's wrong :
DEMO HERE
$(document).ready(function () {
//EnablePickTargetButton();
//clear contents; use a delay because tinyMCE editor isn't always fully loaded on document.ready
var t = setTimeout(function () {
if ($('#textEditorForCreate').length != 0 && tinymce.editors.length > 0)
tinyMCE.activeEditor.setContent('');
}, 300);
if($('#txtSearchUser').length!=0)
{
$('#txtSearchUser').keyup(function (e) {
if (e.keyCode == 13) {
e.preventDefault();
searchUser();
}
else
alert('cucu');
});
}
});