JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 22nd, 2012, 04:55 AM
rageedi rageedi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2012
Posts: 13 rageedi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 22 m 57 sec
Reputation Power: 0
Setting value to a text box

hi guys,

i want to set a value to a text box using javascript
i want to use the getElementById function,

this is what i have so far but its not w0rking, any ideas?

Code:
<html>
<head>
	<script type = "text/javascript">
	document.getElementById('fname').value = 'rageedi';
	//return false;
	</script>
</head>


<body>
<form name="myform" action="javaScriptExample3.html">
	<input type = "text" name ="firstname" id="fname">
	<input type = "text" name ="lasttname" id="lname">
	<input type = "submit" name="submit" value = "submit">
</form>
</body>

</html>

Reply With Quote
  #2  
Old October 22nd, 2012, 05:24 AM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,931 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 12 h 15 m 29 sec
Reputation Power: 2571
Your syntax is correct, however you cannot assign a value to an element that does not yet exist. The document loads line by line, so when it reaches your Javascript the input has not yet been created and it will throw an error (Not likely to be seen). You can either move your script below the input element, not something I would personally do, or apply an event listen, as follows.
Javascript Code:
Original - Javascript Code
  1. onload = startup; // When the document has finished loading, run the "startup" function.
  2.  
  3. function startup() {
  4.     document.getElementById('fname').value = 'rageedi';
  5. }
Obviously if you are going to be calling this script from a click or form submission, then it will no longer be an issue.
__________________
[PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

W3Fools - A W3Schools Intervention.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Setting value to a text box

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap