How to set focus using javascript?

Discussion in 'JavaScript' started by Hitul, Mar 29, 2007.

  1. #1
    Hi. I have made one login form and i want that if the user doesn't enter user name or password then the cursor should directly set to the empty field. How to do it? I have checked for blank field but i am not able to set focus. Please tell me how to do it? Thanks in advance.
     
    Hitul, Mar 29, 2007 IP
  2. bibel

    bibel Active Member

    Messages:
    289
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    58
    #2
    document.getElementById('fieldId').focus()
     
    bibel, Mar 29, 2007 IP
  3. mad4

    mad4 Peon

    Messages:
    6,986
    Likes Received:
    493
    Best Answers:
    0
    Trophy Points:
    0
    #3
    This sends the cursor to the field that was not complete and turns the field pink.

    	if (document.yourform.fieldname.value == "") {
    		alert("You have not filledin this field.");
    		document.yourform.fieldname.focus();
    		if(document.all || document.getElementByID){
    	    document.yourform.fieldname.style.background = "pink";
    	}
    		return false;
    	}
    Code (markup):
     
    mad4, Mar 29, 2007 IP
  4. Hitul

    Hitul Peon

    Messages:
    76
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank you...... you have given the perfect solution.......
     
    Hitul, Mar 29, 2007 IP
  5. ztoma

    ztoma Peon

    Messages:
    43
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ztoma, Mar 30, 2007 IP
  6. Simbu

    Simbu Greenhorn

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #6
    its useful.thank you for posting
     
    Simbu, Sep 26, 2013 IP