$(document).keydown(function(e){
if (e.keyCode == 40 && $('img.something').length > 0) {
setNextPage('720/GIJoe/main.txt', 'ele1');
return false;
}
});
Please help me write this jQuery statement
-
31-05-2022 - |
سؤال
If there is an image on the document that has a class of "something" and the down arrow (key 40) is pressed then run function .setNextPage.
Here is the code I have so far: (the down arrow to trigger the function works, just need to add in the need for the second condition to be met)
$(document).keydown(function(e){
if (e.keyCode == 40) {
setNextPage('720/GIJoe/main.txt', 'ele1');
return false;
} });
Thanks for any help!
المحلول
نصائح أخرى
if (e.keyCode == 40 && $('.something').length > 0) {
You can check if theres an element existing in the DOM by using the native javascript .length function.
if(e.keyCode == 40 && $('img.something').length) {
//code
}
if there isn't an img element with the class of something, it will return false and the statement won't run.
لا تنتمي إلى StackOverflow