/    Sign up×
Community /Pin to ProfileBookmark

I have an input textbox that has an id of “loginview:frmLogin:j_username”. How can I get the cursor to show up in this box when the page loads? I’ve tried document.loginview:frmLogin:j_username.focus();, but that doesn’t seem to work. I cannot change the id or name (which is the same) of the textbox, as we are using java (jsf) and the id/name appends the form name, etc. to the id/name of the input box.

Thanks.

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@EmerickOct 08.2007 — Hopefully this does it ?

<i>
</i>&lt;head&gt;
&lt;!-- set focus to a field with the name "searchcontent" in my form --&gt;
&lt;script type="text/javascript"&gt;
function setfocus(a_field_id) {
$(a_field_id).focus()
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body onload="setfocus('customervalue');"&gt;

Customer: &lt;input name="customer" id="customervalue" /&gt;

&lt;/body&gt;
Copy linkTweet thisAlerts:
@jrthor2authorOct 08.2007 — That didn't seem to do anything. Here is my page code:
<i>
</i> &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;&lt;head&gt;&lt;meta content="text/html; charset=UTF-8" http-equiv="Content-Type"/&gt;&lt;link href="/www.riteaid.com/w-content/css/main.css" media="screen" rel="stylesheet" type="text/css"/&gt;&lt;link href="/www.riteaid.com/w-content/css/default-fonts.css" media="screen" rel="stylesheet" title="default" type="text/css"/&gt;&lt;link href="/www.riteaid.com/w-content/css/large-fonts.css" media="screen" rel="alternate stylesheet" title="large" type="text/css"/&gt;
&lt;script language="Javascript" type="text/javascript" src="/www.riteaid.com/w-content/scripts/main.js"&gt;&lt;/script&gt;
&lt;script language="Javascript" type="text/javascript" src="/www.riteaid.com/w-content/scripts/myriteaid/myriteaid.js"&gt;&lt;/script&gt;
&lt;script language="JavaScript" type="text/javascript" src="/www.riteaid.com/w-content/scripts/styleswitcher.js"&gt;&lt;/script&gt;
&lt;title&gt;Login&lt;/title&gt;&lt;script type="text/javascript"&gt;
function setfocus(a_field_id) {
$(a_field_id).focus()
}
&lt;/script&gt;&lt;/head&gt;
&lt;body onload="setfocus('loginview:frmLogin:j_username');"&gt;&lt;script type="text/javascript"&gt;
function readcookie(cookieName){
document.cookie="cookiesEnabled=true";
var start = document.cookie.indexOf(cookieName);
if (start == -1){
window.location = "browser_settings.jsf";
}
}
readcookie("cookiesEnabled");

<i> </i> if (top != window) {
<i> </i> top.location.href="/myriteaid/login.jsf";
<i> </i> }
<i> </i> &lt;/script&gt;&lt;noscript&gt;&lt;meta content="0;url=browser_settings.jsf" http-equiv="refresh"/&gt;&lt;/noscript&gt;&lt;div id="wrapper"&gt;&lt;div id="headerBlock"&gt;&lt;div id="utiLBlock"&gt;&lt;div id="inputFields"&gt;&lt;div id="searchBox"&gt;&lt;form action="/sitesearch/index.jsf" id="raSearch" method="post" name="raSearch"&gt;&lt;label for="Search"&gt;&lt;span id="header:searchSiteText" title="Search site"&gt;Search site &lt;/span&gt;&lt;input class="inputFixed" id="Search" name="searchCriteria" size="10" title="Search RiteAid.com" type="text"/&gt;&lt;/label&gt; &lt;input name="checkAll" type="hidden" value="true"/&gt;&lt;input alt="Search RiteAid.com" class="inputButton" name="Search" type="submit" value="Search"/&gt;&lt;/form&gt;&lt;/div&gt;&lt;div id="zipBox"&gt;&lt;form action="/stores/locator/store_results.jsf" id="zipSearch" method="get" name="raStoreSearch"&gt;&lt;label for="header:zip" style="font-weight: bold;"&gt;

Find a store &lt;/label&gt;&lt;input id="header:zip" type="text" name="header:zip" value="Enter Zip" class="cleardefault" maxlength="10" size="8" title="Find a store" /&gt; &lt;input name="streetaddress" type="hidden" value=""/&gt;&lt;input name="city" type="hidden" value=""/&gt;&lt;input name="state" type="hidden" value=""/&gt;&lt;input name="storeum" type="hidden" value="0"/&gt;&lt;input name="miles" type="hidden" value="20"/&gt;&lt;input name="maxshow" type="hidden" value="10"/&gt;&lt;input name="platlong" type="hidden" value="0.0~0.0~0.0~0.0"/&gt;&lt;input name="conv" type="hidden" value="false"/&gt;&lt;input name="digital" type="hidden" value="false"/&gt;&lt;input name="drive" type="hidden" value="false"/&gt;&lt;input name="gnc" type="hidden" value="false"/&gt;&lt;input name="nite" type="hidden" value="false"/&gt;&lt;input name="plab" type="hidden" value="false"/&gt;&lt;input name="pOnline" type="hidden" value="false"/&gt;&lt;input name="pic" type="hidden" value="false"/&gt;&lt;input name="rewards" type="hidden" value="false"/&gt;&lt;input alt="Find a Rite Aid" class="inputButton" name="Go" type="submit" value="Go"/&gt;&lt;/form&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;form id="loginForm" method="post" action="/myriteaid/ibm_security_logout/"&gt;&lt;input type="hidden" name="logoutExitPage" value="/login.jsf" /&gt; &lt;/form&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="mainBlock"&gt;&lt;div id="lhsBlock"&gt;&lt;div class="mraRedCap"&gt; &lt;/div&gt;
&lt;div id="lhsBox"&gt;&lt;div class="lhsCatSlct"&gt;&lt;h3&gt;MyRite&amp;shy;Aid.com&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;span id="lhsview:signUpText" title="Sign up now for:"&gt;Sign up now for:&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;a id="lhsview:signInLink1" href="/myriteaid/pharmacy_choices/pharmacy_profile.jsf" title="Online Rx records"&gt;&lt;span id="lhsview:txtsignInlnk1" title="Online Rx records"&gt;Online Rx records&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a id="lhsview:signInLink2" href="/myriteaid/mycoupons.jsf" title="Online Coupons"&gt;&lt;span id="lhsview:txtsignInlnk2" title="Online Coupons"&gt;Online Coupons&lt;/span&gt;&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="content"&gt;&lt;div id="cntSubBlock"&gt;&lt;div id="mainPromoSubBlock"&gt;&lt;img id="imgGenericOffer" src="../www.riteaid.com/w-content/images/myriteaid/generic/cnfg_hdr_generic.png" alt="Take advantage of all MyRiteAid.com benefits! Register Today." /&gt;&lt;/div&gt;&lt;div id="promoSubRow1"&gt;&lt;div class="cntBlockSub leftCol"&gt;&lt;h3 id="txtNotReg"&gt;&lt;span id="txtLftCol"&gt;Not Registered?&lt;/span&gt;&lt;/h3&gt;&lt;p align="center"&gt;&lt;img id="imgMyRiteAid" src="../www.riteaid.com/w-content/images/myriteaid/generic/mra_logo_sm.png" alt="MyRiteAid.com" height="65" width="140" /&gt;&lt;/p&gt;&lt;form id="frmVerification" method="post" action="/myriteaid/login.jsf" enctype="application/x-www-form-urlencoded"&gt;

&lt;p align="center"&gt;&lt;a id="frmVerification:lnkGenericVerification" href="#" onclick="document.forms['frmVerification']['__LINK_TARGET__'].disabled=false;document.forms['frmVerification']['__LINK_TARGET__'].value='frmVerification:lnkGenericVerification'; document.forms['frmVerification'].submit();document.forms['frmVerification']['__LINK_TARGET__'].disabled=true; return false;"&gt;&lt;span id="frmVerification:txtSigup" class="blueTextButton"&gt;Register Now!&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;input type="hidden" name="frmVerification" value="frmVerification" /&gt;&lt;input type="text" style="display:none" disabled name="__LINK_TARGET__" /&gt;&lt;/form&gt;&lt;/div&gt;&lt;div class="cntBlockSub rightCol"&gt;&lt;form id="loginview:frmLogin" method="post" action="/myriteaid/login.jsf" enctype="application/x-www-form-urlencoded"&gt;
&lt;h3&gt;Already registered?&lt;/h3&gt;&lt;input id="loginview:frmLogin:scode" type="hidden" name="loginview:frmLogin:scode" value="0" /&gt;&lt;table border="0" cellpadding="0" cellspacing="3" width="98%"&gt;&lt;tr&gt;&lt;td colspan="2"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="right" valign="middle"&gt;&lt;p&gt;&lt;span id="loginview:frmLogin:txtoutLoginPrompt"&gt;User ID:&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td align="left" valign="middle"&gt;&lt;input id="loginview:frmLogin:j_username" type="text" name="loginview:frmLogin:j_username" alt="Username" maxlength="15" size="16" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 11px" /&gt;&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td align="right" valign="middle"&gt;&lt;p&gt;&lt;span id="loginview:frmLogin:txtoutLoginPasswordPrompt"&gt;Password:&lt;/span&gt;&lt;/p&gt;&lt;/td&gt;&lt;td align="left" valign="middle"&gt;&lt;input id="loginview:frmLogin:j_password" type="password" name="loginview:frmLogin:j_password" value="" alt="Password" maxlength="15" size="16" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 11px" /&gt;&lt;input name="specialPromo" type="hidden" value=""/&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;p align="center"&gt;&lt;input type="submit" value="Login" name="loginview:frmLogin:btnLogin" id="loginview:frmLogin:btnLogin" class="inputButton" /&gt;&lt;/p&gt;&lt;p align="center"&gt;&lt;a id="loginview:frmLogin:lnkForgotPassword" href="#" style="color: #003273;" onclick="document.forms['loginview:frmLogin']['__LINK_TARGET__'].disabled=false;document.forms['loginview:frmLogin']['__LINK_TARGET__'].value='loginview:frmLogin:lnkForgotPassword'; document.forms['loginview:frmLogin'].submit();document.forms['loginview:frmLogin']['__LINK_TARGET__'].disabled=true; return false;"&gt;&lt;span id="loginview:frmLogin:txtoutForgotPasswordLink"&gt;Forgot Your User ID or Password?&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p align="center"&gt;&lt;input type="image" src="../www.riteaid.com/w-content/images/myriteaid/livingmore/signUpNow.jpg" name="loginview:frmLogin:btnCalLMLogin" id="loginview:frmLogin:btnCalLMLogin" alt="California Living More Members Sign Up Now." /&gt;&lt;/p&gt;&lt;input type="hidden" name="loginview:frmLogin" value="loginview:frmLogin" /&gt;&lt;input type="text" style="display:none" disabled name="__LINK_TARGET__" /&gt;&lt;/form&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="promoSubRow4"&gt;&lt;div id="footer"&gt;&lt;p&gt;&lt;a id="myRadFooterSubView:ftrLinkHome" href="/" class="ftrHome"&gt;&lt;span id="myRadFooterSubView:ftrLinkHomeText"&gt;Home&lt;/span&gt;&lt;/a&gt;&lt;a id="myRadFooterSubView:ftrLinkTerms" href="/home/terms_conditions.jsf"&gt;&lt;span id="myRadFooterSubView:ftrLinkTermsText"&gt;Terms &amp;amp; Conditions&lt;/span&gt;&lt;/a&gt;&lt;a id="myRadFooterSubView:ftrLinkRAPrivacy" href="/home/privacy.jsf"&gt;&lt;span id="myRadFooterSubView:ftrLinkRAPrivacyText"&gt;RiteAid.com Privacy&lt;/span&gt;&lt;/a&gt;&lt;a id="myRadFooterSubView:ftrLinkPatientPrivacy" href="/home/privacy_practices.jsf"&gt;&lt;span id="myRadFooterSubView:ftrLinkPatientPrivacyText"&gt;Patient Privacy&lt;/span&gt;&lt;/a&gt;&lt;a id="myRadFooterSubView:ftrLinkHelp" href="/home/help.jsf"&gt;&lt;span id="myRadFooterSubView:ftrLinkHelpText"&gt;Help&lt;/span&gt;&lt;/a&gt;&lt;/p&gt;&lt;p&gt;© 2001-2007 Rite Aid Corp. All rights reserved.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id="rhsBlock"&gt;&lt;div class="relatedBox"&gt;&lt;div class="relatedCat"&gt;&lt;h3&gt;Related Information&lt;/h3&gt;&lt;ul&gt;&lt;li&gt;&lt;a id="lnkScr" href="/redir/?go=aHR0cHM6Ly9yaXRlYWlkLnJlYmF0ZXBsdXMuY29tLw==&amp;amp;from=aHR0cDovLw==Y2FyLnJpdGVhaWQuY29tL215cml0ZWFpZC9sb2dpbi5qc3A=" target="scr"&gt;Single Check Rebates&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a id="lnkDMgmt" href="http://www.riteaiddiabetes.com" title="Diabetes Management"&gt;Diabetes Management&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a id="lnkNPdt" href="/stores/whats_new/" title="New Products"&gt;New Products&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a id="lnkStoreLocator" href="/stores/locator/" title="Store Locator"&gt;Store Locator&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a id="lnkContactUs" href="/contact_us/" title="Contact Us"&gt;Contact Us&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="rhsBox"&gt;&lt;a id="lnkLvmr" href="/pharmacy/livingmore/" title="Living More"&gt;&lt;img id="imgLvmr" src="/myriteaid/../www.riteaid.com/w-content/images/rhs_ad_living_more.png" alt="Living More" height="174" title="Living More" width="120" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;
&lt;/body&gt;&lt;/html&gt;
Copy linkTweet thisAlerts:
@WebnerdOct 08.2007 — <script type="text/javascript">

window.onload=function(){

document.getElementById('loginview:frmLogin:j_username').focus();

}

</script>

Make sure your field has an ID attribute:

id="loginview:frmLogin:j_username"

[b]Emerick[/b], you can't always assume a user has Prototype or jQuery loaded :-)
Copy linkTweet thisAlerts:
@jrthor2authorOct 08.2007 — I hate to say it, but that didn't help either. My input field looks like this:

<input id="loginview:frmLogin:j_username" type="text" name="loginview:frmLogin:j_username" alt="Username" maxlength="15" size="16" style="font-family: verdana, arial, helvetica, sans-serif; font-size: 11px" />
Copy linkTweet thisAlerts:
@WebnerdOct 08.2007 — Are there any other scripts that are loading that could be overwriting the onload function? The other function worked fine for me by changing the :

$(

to

document.getElementById(
Copy linkTweet thisAlerts:
@jrthor2authorOct 08.2007 — Yes, I think there is another script that is included in this page that does a window.onload.
Copy linkTweet thisAlerts:
@WebnerdOct 08.2007 — Ok, then, you may need another solution since you can't onload the focus. Instead, just include the JS inline near the closing </body> tag.

<i>
</i>&lt;script type="text/javascript"&gt;
document.getElementById('loginview:frmLogin:j_username').focus();
&lt;/script&gt;


Or you can change the other Js file and add a new window.onload handler but this should be a quick solution
Copy linkTweet thisAlerts:
@jrthor2authorOct 08.2007 — Actually, I can't find any other window.onload function in the scripts that are included
Copy linkTweet thisAlerts:
@jrthor2authorOct 08.2007 — putting it inline at the bottom of my page didn't work either.
Copy linkTweet thisAlerts:
@WebnerdOct 08.2007 — Are you sure you get no Javascript errors on the page? Try this:

<i>
</i>setTimeout(function(){
document.getElementById('loginview:frmLogin:j_username').focus();
},100);
Copy linkTweet thisAlerts:
@jrthor2authorOct 08.2007 — Ah, I did have an error. I forgot to remove a } at the end. It is working fine now, thanks!
×

Success!

Help @jrthor2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...