/    Sign up×
Community /Pin to ProfileBookmark

Changing <input> properties with setAttribute

Hello,

I’m trying to change these attributes of the input tag. Why doesn’t it work?

[code]
<html>

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=windows-1252″>
<title>New Page 1</title>
<script type=”text/javascript”>
function init(){
var input = document.getElementById(“input”);
input.setAttribute(“name”, “CODE”);
input.setAttribute(“value”,”CODE”);
}
</script>
</head>

<body onload=”init()”>
<form>
<input id=”input” type=”hidden” name=”” value=””>
</form>
</body>
</html>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@Angry_Black_ManJul 30.2007 — worked for me without any changes except alerting the new values to make sure they are different than their old values. i expect that you think the source code would change. it doesnt. it merely does what you said to do "behind the scenes" as it were:

&lt;html&gt;

&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1252"&gt;
&lt;title&gt;New Page 1&lt;/title&gt;
&lt;script type="text/javascript"&gt;
function init(){
var input = document.getElementById("input");
input.setAttribute("name", "javascript created name");
input.setAttribute("value","javascript created value");
}
&lt;/script&gt;
&lt;/head&gt;

&lt;body onload="init()"&gt;
&lt;form&gt;
&lt;input id="input" type="hidden" name="HTML defined name" value="HTML defined value"&gt;
&lt;/form&gt;

&lt;div onclick="alert(document.getElementById('input').getAttribute('name'));"&gt;click to alert the name after the javascript ran..&lt;/div&gt;

&lt;div onclick="alert(document.getElementById('input').getAttribute('value'));"&gt;click to alert the value after the javascript ran..&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@nx6310authorJul 30.2007 — Alright, thanks for the fast response. This was a beginning to the script I'm trying to write. I hope you can help me finish this, if possible.

The code input.setAttribute("value","javascript created value");

The javascript created value... I want that value to be taken from a cookie that's on the user's computer. I really have no idea where to start, and would appreciate any help at all.

Thanks a lot.
Copy linkTweet thisAlerts:
@DokJul 30.2007 — You don't have to use the setAttribute method. You can just do
[CODE]input.value = "javascript created value";[/CODE]
Here is a list of cookie tutorials: http://www.google.dk/search?q=javascript+cookie+tutorial
Copy linkTweet thisAlerts:
@nx6310authorJul 31.2007 — Okay, so I've got this. Now, I think this does what I want, unless I'm wrong? My cookie's name is pizza, and the value that it has I want it to be the value in the input id=sd. Am I close?

&lt;html&gt;

&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=windows-1252"&gt;
&lt;script type="text/javascript"&gt;

function getCookie(pizza) {
var dc = document.cookie;
var cname = name + "=";

<i> </i>if (dc.length &gt; 0) {
<i> </i> begin = dc.indexOf(cname);
<i> </i> if (begin != -1) {
<i> </i> begin += cname.length;
<i> </i> end = dc.indexOf("", begin);
<i> </i> if (end == -1) end = dc.length;
<i> </i> return unescape(dc.substring(begin, end));
<i> </i> }
<i> </i> }
<i> </i>return null;
<i> </i>}
<i> </i>&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;form action="post"&gt;
&lt;textarea rows="8" cols="40"&gt;&lt;/textarea&gt;&lt;br&gt;
&lt;input id="sd" type="hidden" name="sid" value=""&gt;
&lt;input type="hidden" name="mode" value="reply" /&gt;
&lt;input type="hidden" name="t" value="18766" /&gt;
&lt;input type="submit" onclick="this.form.sd.value = getCookie(pizza)" name="post" value="Submit" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;


Or wait! Is this what I want to use? I am so confused, can anybody please help?

<i>
</i>function getCookie(NameOfCookie)
{ if (document.cookie.length &gt; 0)
{ begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1)
{ begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null;
}
×

Success!

Help @nx6310 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.28,
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,
)...