Forum Moderators: open

Message Too Old, No Replies

How do I set the value of a field that has a space in the name?

need to be able to set a value of a form field whose name includes a space

         

jacosta000

6:19 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Hello,

I'm trying to set the value of a field in a form that has a space character in the name.

I'm trying to do something like this:
Code:

document.forms["otherform"].phone.value=document.forms["survey"].yourphone.value;

The problem is that 'phone' is actually supposed to be 'custom Phone'. When I try that, it doesn't work, though. The reason why I have to have a space in the name is because the form element is tied to our mass email distribution system (Aweber) and this particular element was created with a space, and cannot be changed.

So I'm thinking that hopefully there is a special character I'm supposed to use besides the space character to set the value? Unfortunately, I'm stumped.

Anyone have any ideas?

Thanks!
*j

rocknbil

6:33 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Referencing form objects by name from within the browser has long since been deprecated for document.getElementById:

<input type="text" name="Your Phone" id="other-phone">
........
<input type="text" name="Your Phone" id="survey-phone">

document.getElementById('other-phone').value=document.getElementById('survey-phone').value;

If you absolutely can't access the form fields, you could do some workarounds, for example, iteration through the form element's index in the source:

document.forms["otherform"].elements[3].value=document.forms["survey"].elements[6].value;

This gets to be a nightmare to maintain if the form fields move around at all.

Fotiman

6:47 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Just as you have referenced the form name using array notation, so can you reference the named inputs. Dot notation and array notation are interchangeable.

Try this:

document.forms["otherform"]["custom Phone"].value

[edited by: Fotiman at 6:47 pm (utc) on Aug. 14, 2008]

jacosta000

8:02 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



Unfortunately, none of these ideas work. I tried all three suggestions and all of them fail passing data to aweber. If I remove that line of code, it goes through fine :(

DrDoc

8:26 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Impossible. Accessing the elements array I know for sure will work. Fotiman's suggestion should work as well (even though I haven't actually tried it).

There must be something else that's wrong!

jacosta000

8:39 pm on Aug 14, 2008 (gmt 0)

10+ Year Member



I definitely agree that ALL 3 suggestions should have worked, but I think the problem may have more to do with aweber's 'spaced out' names...

the page I'm working with was created by using a program called 'Psychic Sales Letter'.

The page asks a series of questions in a survey format and has a form at the bottom (name, email, phone). When the button is clicked, it triggers an onClick event(it does not do a form submit. If that was the case, I don't think this would be an issue).

there is an aweber form that looks like this:
<form method="post" name="otherform" target="otherformsubmit" action="http://www.aweber.com/scripts/addlead.pl">
<input name="name" type="hidden">
<input name="from" type="hidden">
<input name="phone" type="hidden">
<input type="hidden" name="custom Phone" id="custom Phone">
<input type="hidden" name="meta_web_form_id" value="1683544204">
<input type="hidden" name="meta_split_id" value="">
<input type="hidden" name="unit" value="test">
<input type="hidden" name="redirect" value="http://www.myDomain.com/signup/thank-you.html">
<input type="hidden" name="meta_redirect_onlist" value="">
<input type="hidden" name="meta_adtracking" value="">
<input type="hidden" name="meta_message" value="1">
<input type="hidden" name="meta_required" value="from,name,phone">
<input type="hidden" name="meta_forward_vars" value="0">
</form>

there is some javascript code that takes values from the initial form and passes them to the aweber form:
document.forms["otherform"].name.value=document.forms["survey"].yourname.value;
document.forms["otherform"].from.value=document.forms["survey"].youremail.value;
document.forms["otherform"].phone.value=document.forms["survey"].yourphone.value;

Then, the aweber form is submitted:
document.forms["otherform"].submit();

When I try and do anything to the code that passes the phone value between forms, none of the data is submitted at all. If i leave it as-is, it's fine. I was able to confirm that I can hardcode a value for phone within the aweber form, and it still gets passed.

I'm not sure what the problem is, but it's definitely very odd...

DrDoc

11:08 pm on Aug 14, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't see how it can not work.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
<html>
<head>
<title>Untitled</title>
</head>
<body onload="loiloi()">
<form name="foo">
<input name="bar" value="bar">
<input name="baz bork" value="baz bork">
</form>
<script type="text/javascript">
function loiloi() {
alert(document.forms['foo'].bar.value);
document.forms['foo'].bar.value = "meh";
alert(document.forms['foo'].bar.value);
alert(document.forms['foo']['baz bork'].value);
document.forms['foo']['baz bork'].value = "mih moh";
alert(document.forms['foo']['baz bork'].value);
}
</script>
</body>
</html>

You should be able to access/modify the element and its value as needed.

[edited by: DrDoc at 11:11 pm (utc) on Aug. 14, 2008]

jacosta000

1:02 pm on Aug 15, 2008 (gmt 0)

10+ Year Member



well... i tried again this morning, and it's now working. Either I had my quotes mixed up or aweber was just not accepting my changes.

Thanks for all your help!