Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

JS focus() method

Status
Not open for further replies.

KingSlick

Programmer
Mar 9, 2007
45
US
ok, I have a text box that has some AJAX search_suggest built into it. When you click on a suggestion, the text box is populated with the suggestion that was clicked and the focus is also sent back to the text box. However, it is going to the begining of the text and I would like for it to go to the end of the text. Code follows...
Code:
//Click function
function setSearch(field, value,div) {
	var current = document.getElementById(field).value;
	if (current.indexOf('\"') == -1 ) {
      document.getElementById(field).value = '\"' + value + '\"';
	  document.getElementById(field).focus();
	}
	else {
	  var lastQuote = current.lastIndexOf('\"');
	  current = current.substr(0,lastQuote + 1);
	  document.getElementById(field).value = current + '\"' + value + '\"';
	  document.getElementById(field).focus();
	}
    document.getElementById(div).innerHTML = '';
}
It works fine in FF, but I need for it to work in IE6 and IE7

Any suggestions??
 
Do this:

Switch up the value assigning and the focus
Code:
//Click function
function setSearch(field, value,div) {
    var current = document.getElementById(field).value;
    if (current.indexOf('\"') == -1 ) {
      [!]document.getElementById(field).focus();
      document.getElementById(field).value = '\"' + value + '\"';[/!]
    }
    else {
      var lastQuote = current.lastIndexOf('\"');
      current = current.substr(0,lastQuote + 1);
      document.getElementById(field).focus();
      [!]document.getElementById(field).value = current + '\"' + value + '\"';[/!]
    }
    document.getElementById(div).innerHTML = '';
}

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top