By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,317 Members | 1,312 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,317 IT Pros & Developers. It's quick & easy.

missing name attribute from dynamic input element

P: n/a
I'm attempting to submit a form via a function which dynamically creates a
hidden input:

function submitLocation(theForm) {
var e = document.createElement('input');
e.setAttribute('type', 'hidden');
e.setAttribute('name', 'location');
e.setAttribute('value', '1');
var f = document.getElementById(theForm);
f.appendChild(e);
document.forms[theForm].submit();
}

The function is called from an anchor's onclick event:

<form name='form1' method='post' action='action.php'>
<a onclick="submitLocation('form1');">Submit</a>
</form>

However the function produces an input which is missing the name element:

<INPUT type=hidden value=1>

The desired output should be:

<INPUT type=hidden name=location value=1>

Can someone give me a clue? Thanks in advance.
Sep 28 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

Bosconian wrote:
I'm attempting to submit a form via a function which dynamically creates a
hidden input:

function submitLocation(theForm) {
var e = document.createElement('input');
e.setAttribute('type', 'hidden');
e.setAttribute('name', 'location');
e.setAttribute('value', '1');
var f = document.getElementById(theForm);
f.appendChild(e);
document.forms[theForm].submit();
}
Your code worked for me in IE6. I got a POST with "location = 1". If
it's not working for you, perhaps change "location" to a different
name, just in case somewhere else in your code it's getting mixed up
with window.location?

Kev

Sep 28 '06 #2

P: n/a
Bosconian said the following on 9/27/2006 10:19 PM:
I'm attempting to submit a form via a function which dynamically creates a
hidden input:

function submitLocation(theForm) {
var e = document.createElement('input');
e.setAttribute('type', 'hidden');
e.setAttribute('name', 'location');
e.setAttribute('value', '1');
<snip>
Can someone give me a clue? Thanks in advance.
Don't use setAttribute, access the value directly:

e.name="location";

Not sure "e" is a good choice of variable name either.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 28 '06 #3

P: n/a
"Kevin Darling" <kd******@basit.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>
Bosconian wrote:
>I'm attempting to submit a form via a function which dynamically creates
a
hidden input:

function submitLocation(theForm) {
var e = document.createElement('input');
e.setAttribute('type', 'hidden');
e.setAttribute('name', 'location');
e.setAttribute('value', '1');
var f = document.getElementById(theForm);
f.appendChild(e);
document.forms[theForm].submit();
}

Your code worked for me in IE6. I got a POST with "location = 1". If
it's not working for you, perhaps change "location" to a different
name, just in case somewhere else in your code it's getting mixed up
with window.location?

Kev
Thanks for your response and testing the code.

I extracted the code from my project and you're right, it does work in both
IE6 and Firefox.

The problem isn't with the syntax, variable names or parameter usage. I
didn't mention that this code is being used with Drupal and somehow the POST
data is being altered. I need to do some more investigating.
Sep 28 '06 #4

P: n/a
"Bosconian" <no****@nowhere.comwrote in message
news:v9******************************@comcast.com. ..
"Kevin Darling" <kd******@basit.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>>
Bosconian wrote:
>>I'm attempting to submit a form via a function which dynamically creates
a
hidden input:

function submitLocation(theForm) {
var e = document.createElement('input');
e.setAttribute('type', 'hidden');
e.setAttribute('name', 'location');
e.setAttribute('value', '1');
var f = document.getElementById(theForm);
f.appendChild(e);
document.forms[theForm].submit();
}

Your code worked for me in IE6. I got a POST with "location = 1". If
it's not working for you, perhaps change "location" to a different
name, just in case somewhere else in your code it's getting mixed up
with window.location?

Kev

Thanks for your response and testing the code.

I extracted the code from my project and you're right, it does work in
both IE6 and Firefox.

The problem isn't with the syntax, variable names or parameter usage. I
didn't mention that this code is being used with Drupal and somehow the
POST data is being altered. I need to do some more investigating.

I couldn't get Drupal to behave nicely with the aforementioned method (which
works perfectly fine outside of Drupal) and opted instead to simply hardcode
the hidden input into the form and submit the form via an anchor hyperlink
like so:

<code>
<a href='javascript:form1.submit()'>Submit</a>
</code>
Sep 28 '06 #5

P: n/a
Bosconian said the following on 9/28/2006 10:53 AM:

<snip>
>
I couldn't get Drupal to behave nicely with the aforementioned method (which
works perfectly fine outside of Drupal) and opted instead to simply hardcode
the hidden input into the form and submit the form via an anchor hyperlink
like so:

<code>
<a href='javascript:form1.submit()'>Submit</a>
</code>
Oh geez, from bad to worse.

http://jibbering.com/faq/#FAQ4_24

form1.submit() is an IE only coding habit.

If you want to submit a form, just use a submit button.....

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Sep 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.