SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: changing focus

  1. #1
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    changing focus

    In my javascript form validation I have a prompt that asks the user for input if a field is left blank. The form is quite long and what I'd like to do is bring the user back to that particular field to show them that it is blank. Is there a way to do such a thing? Is it really needed?

    I'm just getting into javascript and the references I have don't explain some of these things.

    Also if anyone has a good reference that i can look up on my own that would be great too.

    Here is my validation as it is now:

    Code:
    function qval() {
    	//check the name
    	if (document.dq.name.value.length == 0) {
    		var newname = window.prompt("Please enter your name");
    		document.dq.name.value = newname;
    		return false;
    	}
    	// check the email
    		if (document.dq.email.value.length == 0) {
    		var newemail = window.prompt("Please enter your email address");
    		document.dq.email.value = newemail;
    		return false;
    	}
    	// check the city
    		if (document.dq.city.value.length == 0) {
    		var newcity = window.prompt("Please enter your city");
    		document.dq.city.value = newcity;
    		return false;
    	}
    	// check the state
    		if (document.dq.state.value.length == 0) {
    		var newstate = window.prompt("Please enter your state");
    		document.dq.state.value = newstate;
    		return false;
    	}
    	// country
    		if (document.dq.country.value.length == 0) {
    		var newcountry = window.prompt("Please enter your country");
    		document.dq.country.value = newcountry;
    		return false;
    	}
    	return true;
    }
    Thanks in advance.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2006
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    document.getElementById("foo").focus();
    this what u need?

  3. #3
    SitePoint Aficionado JVLB's Avatar
    Join Date
    Jan 2002
    Location
    N 44 56.537' W 123 3.683'
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    While focus() will (except in the case of some browsers with focus bugs) bring focus to the element, it won't necessarily scroll to it. For that you need an HTML anchor tag.

  4. #4
    SitePoint Zealot
    Join Date
    Apr 2005
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks everyone - that is what I needed.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •