tags:

views:

66

answers:

4
 var sb = document.getElementById("top_search_box");
        var val = sb.value;
        if(!val) val = "";
        val = val.replace(/^[ ]+/g, "").replace(/[ ]+$/g, "");
        if(val == "" || val=="Search for Items") {
            sb.focus();
            return false;
        }
        return true;
+1  A: 

The code checks if an input field with an ID=top_search_box has any value and if it doesn't then updates the input field value to "Search for Items"

Andris
+2  A: 

It gets rid of an empty value or trailing blanks in the value from an element (probably a text input), and then focuses it if it contains the default or an empty value.

Ignacio Vazquez-Abrams
+1  A: 
val = val.replace(/^[ ]+/g, "").replace(/[ ]+$/g, "");

Remove spaces from start and end of value from the top search box

rest of are very simple.

Sadat
+2  A: 

This code checks to see if the search box has user-inputted value. If it does, it returns true. If it doesn't, it focuses on the search box (places the cursor in there) and returns false. Due to the fact that there are return statements, I'm guessing this is code from a function.

var sb = document.getElementById("top_search_box");

The above code gets the search box, and puts a reference to it in the variable sb

var val = sb.value;

This gets the value of the search box, and puts it in the variable val

if(!val) val = "";

If val is not set, this sets it to the empty string

val = val.replace(/^[ ]+/g, "").replace(/[ ]+$/g, "");

This trims any spaces off the beginning and end of val, so all that's left is the actual value, if there is one, or an empty string if it was only spaces.

if(val == "" || val=="Search for Items") {
    sb.focus();
    return false;
}

If, after all of this, val holds the empty string, or the (presumably default) string "Search for Items", the cursor is moved to the search box, and the function returns false.

return true;

Otherwise, the function returns true.

In the end, it seems the function returns true if there is a user inputted value, and false otherwise. This could be useful if you need to know if a user has put anything in the search box.

Ryan Kinal