/    Sign up×
Community /Pin to ProfileBookmark

How to Set Value in input tag

Hello Everyone,

I am trying to set the value of a Input tag with a value retrieved from the cookies. However, it is not working. Here is the script:

[CODE]<html>
<head>
<title>Example</title>
</head>
<body>

<script type=”text/javascript”>
document.cookie = “username=John”;
var user = get_cookie(“username”);
document.write ( “Hi ” + user + “, welcome to my website!” );
oFormObject.elements[“name”].value = user;
</script>

<center> <br><br><br><br>
<form name=”register_complaint” action=”#”>
User Name: <input type=”text” size=”5″ name=”name” > <br>
<input type=”checkbox” name=”remeber”>Remember Me <br>
<input type=”button” name=”submit” value=”Submit Complaint”>
</form>
</center>

<script type=”text/javascript”>

function get_cookie ( cookie_name )
{
var results = document.cookie.match ( ‘(^|;) ?’ + cookie_name + ‘=([^;]*)(;|$)’ );

if ( results )
return ( unescape ( results[2] ) );
else
return “”;
}
</script>

</body>
</html>[/CODE]

Please help me, I’ve been struggling with this issue for a long time.

Cheers

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@basit74Aug 24.2009 — <html>

<body>


<center> <br><br><br><br>
<form name="register_complaint" action="#">
User Name: <input type="text" size="5" name="name" > <br>
<input type="checkbox" name="remeber">Remember Me <br>
<input type="button" name="submit" value="Submit Complaint">
</form>
</center>



<script language="JavaScript">

/*
Define your function first, before you can call it
*/
function get_cookie ( cookie_name )
{
var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

if ( results )
return ( unescape ( results[2] ) );
else
return "";
}

document.cookie = "username=Basit";
// Call your function here.
var user = get_cookie("username");
document.write ( "Hi " + user + ", welcome to my website!" );

//Access form fields like this.
document.forms[0].elements["name"].value = user;


</script>

</body>

</html>
Copy linkTweet thisAlerts:
@tomascholerauthorAug 24.2009 — thanks a lot
Copy linkTweet thisAlerts:
@tomascholerauthorAug 24.2009 — Just one more question:

[CODE]document.forms[0].elements["name"].value = user;[/CODE]

Why do we use forms[0]? What does it mean?
Copy linkTweet thisAlerts:
@basit74Aug 24.2009 — document.forms is an Array that contains all your form, i mean that you defined in your HTML files.

and forms[0] means just the first form in your focument.
×

Success!

Help @tomascholer 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.17,
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,
)...