Link to home
Start Free TrialLog in
Avatar of kbios
kbios

asked on

What is the syntax for passing a javascript variable to an HTML input box?

I'm capturing a localStorage variable that contains a previously entered email address from another page. Upon the entry of this page I want to show the email address as the 'default' value in the text box.

I need to know the syntax of value=variable in the input statement. Here is the HTML code:

<script type="text/javascript">
  var tmpEMAdd = localStorage.EMAdd;
</script>
            
<form action="">
   <fieldset>
     <input type="text" id="txtsea" size=50 value=tmpEMAdd autofocus/><br />
     <input type="button" value=" Save " onClick="setValue()"/>
   </fieldset>      
</form>
Avatar of rajvja
rajvja
Flag of United Kingdom of Great Britain and Northern Ireland image

<script type="text/javascript">
  var tmpEMAdd = localStorage.EMAdd;
txtsea.value=tmpEMAdd;
</script>
           
<form action="">
   <fieldset>
     <input type="text" id="txtsea" size=50  autofocus/><br />
     <input type="button" value=" Save " onClick="setValue()"/>
   </fieldset>      
</form>
Avatar of kbios
kbios

ASKER

Thanks for the suggestion but txtsea.value option did not work. The value was an empty input box. Wouldn't txtsea need created first? I'm hoping for something like value=[var] or value=$var or value={var}. I just don't know the syntax of passing the previously declared variable.
Hi,

  Use the script tag inside the <BODY> tag.

Otherwise HEAD section will execute first and by that time form controls will not be created.
And also, .HTML file will be reloaded everytime you click the button. It will not preserve any form controls values. If you want this functionality, you need to use either submitting the form to the same page and use querystrings to preserve the values
Avatar of kbios

ASKER

The script tag is inside the <body>. I'm ok with it not preserving any form control values. In this case that is what I want. I simply want to 'pre-load' or 'default' the input box with a value that is stored in a localStorage variable. Perhaps I am thinking incorrecly, but isn't there a way to simply pass a variable to the value, or any other attribute for that matter?
ASKER CERTIFIED SOLUTION
Avatar of Kim Walker
Kim Walker
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kbios

ASKER

mediaman - thank you. i will accept your answer as the solution. your answer definitely works, but i am still in a quandry about passing a variable to the vlaue in the input. can it be done or is my approach just simply not going to work? any follow-up thought would be greatly appreciated.
Avatar of kbios

ASKER

thanks
The alternative is not very pretty. It's necessary to interupt the HTML parser to execute the javascript which can't be done in the middle of a tag. But the javascript could be set up to generate the entire input tag. This way, you can leave your variable definition in the head.

<form action="">
	<fieldset>
		< script type='text/ecmascript'>document.write('<input type="text" id="txtsea" size=50 value="'+tmpEMAdd+'" autofocus />');</script>
<br />
		<input type="button" value=" Save " onClick="setValue()" />
	</fieldset>      
</form>

Open in new window

Avatar of kbios

ASKER

wow!  thanks so much for the follow through. it helps with the overall educational process.