0

I have this code (jQuery). Basically I want to reload page if there is input with #SchoolName existing but it misses class="ac_input", IF that class is present, do not reload page. Is this possible?

<input type="text" onfocus="showVal(this.value);" value="" id="SchoolName" size="30" maxlength="50" name="SchoolName" autocomplete="off" class="ac_input">

function autocomplete() {
         $("#SchoolName").autocomplete("ajaxFuncs.php",{cacheLength:1,mustMatch:1,extraParams:{getSchoolName:1}});
    };

$(document).ready(function(){
    setTimeout("autocomplete()", 500);
    // what do i have to add here???
});
1
  • 2
    Don't pass a string to setTimeout.
    – SLaks
    Jan 17, 2012 at 18:26

3 Answers 3

2

Try this

$(document).ready(function(){
    setTimeout(function(){
         autocomplete()
    }, 500);

    // what do i have to add here???
    if($('#SchoolName').length && !$('#SchoolName').hasClass('ac_input')){
        console.log('reload');
        location.reload();
    }

});

As you requested, we can write the above code in pure JavaScript as below.

window.onload = function(){
    setTimeout(function(){
        autocomplete()
    }, 500);

    if(document.getElementById('SchoolName') 
       && document.getElementById('SchoolName').className != 'ac_input'){
        console.log('reload');
        location.reload();
    }
}
3
  • May be the condition is not satisfied so reload is not happening. I have put a log message in the console, can you check it? Jan 17, 2012 at 19:23
  • There is nothing in log, means that the code never got executed :S I don't understand why not :S What would be normal Javascript code and not jquery?
    – Drazek
    Jan 17, 2012 at 20:01
  • You mean the condition or the whole code including $(document).ready()? Jan 17, 2012 at 20:02
0
if (!$('#SchoolName').is('.ac_input'))
    window.location.reload();
1
  • This will return true if #SchoolName does not exist on the page. Jan 17, 2012 at 18:36
0

Using :not() selector

$("#SchoolName:not(.ac_input)").autocomplete("ajaxFuncs.php",{cacheLength:1,mustMatch:1,extraParams:{getSchoolName:1}});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.