views:

64

answers:

2

I have a function that runs on key tab press, it works fine when i put a javascipt alert in between the code, any kind of alert,if i remove the alert it stops working : my function

//Function to set the tab feture for focus to work properly on fields with autosuggestion(location and name)
function setFocusOnTab(name) {


    var focusElement = "";

    if(name == "name") {//For main contact field

        if ($("#email").is(":visible")) {
            $('#email').focus();
        }
    } else if(name == 'location_name') {//For main contact field
        $("#country").focus();
    } else {//For extra contact field


        var outputDataCurrentVal = name.lastIndexOf('record_');


        if(outputDataCurrentVal < 0) {

            //ADDTIONAL CONTACT TAB
            var outputDataCurrentName = name.lastIndexOf('_name_');
            if(outputDataCurrentName >= 0) {
                //Replacing the  name to get location name.
                locName = currentName.replace("_name_","_designation_"); 
                focusElement = locName;
            } else {

                var outputDataCurrentLoc = name.lastIndexOf('_location_');  
                if(outputDataCurrentLoc >= 0) {
                    //Replacing the location name to get country name.
                    countryName = name.replace("_location_","_country_"); 
                    focusElement = countryName;
                }
            }

        } else {


            //Extra CONTACT TAB
            var outputDataCurrentName = name.lastIndexOf('_name_');
            if(outputDataCurrentName >= 0) {
                //Replacing the  name to get location name.
                locName = currentName.replace("_name_","_location_"); 
                focusElement = locName;
            } else {

                var outputDataCurrentLoc = name.lastIndexOf('_location_');  
                if(outputDataCurrentLoc >= 0) {
                    //Replacing the location name to get country name.
                    countryName = name.replace("_location_","_country_"); 
                    focusElement = countryName;

                }
            }
        }



        $("#" + focusElement).focus();
        return false;
    }


}
+3  A: 

Sounds like you need something to halt your code, which is what alert() does.

You may need a callback instead.

alex
yes i think so , thats why i put return false at the end.But that does not worked, but if put alert in place of return false it works.........very starnge to me.
Jos
In place of false? So you remove the false and make an alert? Try returning true is an option.
Gertjan
ok let me try ,i'll get back to you shortly but thanks. :)
Jos
Oh great return true gets code working fine..thanks all
Jos
Well then give me upvotes ;) But good to hear a return true works, is the code called from an event (like a click), that could explain why it is not working when returning a false (this will cause the event to "stop").
Gertjan
+1  A: 

What might be happening with alert() is that calling it causes the current window to lose focus (as focus moves to the new pop-up dialogue box), and after it's finished it re-focuses the window. This will trigger focus and blur events which might confuse your script, and in Safari the window may not re-focus at all.

It's not clear to me what you are doing here... how are you attaching this code to a tab key event? What event is supposed to be cancelled by the return false;? If you are using keypress, then that simply won't get called for the Tab key in IE, Safari or Chrome. If you are using keydown, then cancelling the event won't prevent the tabbing in Opera. And what about Shift-Tab?

Reproducing/altering browser keyboard behaviour is hard: it's much better not to if there's any other way. For making controls like a drop-down suggester work you are probably much better off just setting the declarative tabIndex on the elements concerned and letting the browser work out how to sort out the tabs from there.

bobince