Forum Moderators: open

Message Too Old, No Replies

How to concatenate form fields?

         

chornbeck

8:53 pm on Aug 9, 2006 (gmt 0)

10+ Year Member



I have the following inputs in an HTML form:

<td>(<input class="phonetext" maxlength="3" name="phoneareacode" size="3" type="text" value="">)
<input class="phonetext" maxlength="3" name="phoneexchange" size="3" type="text" value="">-
<input class="phonetext" maxlength="4" name="phonenumber" size="4" type="text" value=""></td>

I would like to combine all three of these values into one hidden field like this:


<input type="hidden" id="phone" name="phone" value="?">

With the? being the three values merged together...

Thanks!

MisYu

11:26 am on Aug 10, 2006 (gmt 0)

10+ Year Member



var f = document.forms["yourFormName"].elements;
var s = "?";
for (var i = 0; i < f.length; i++) {
if (f[i].getAttribute("class") == "phonetext") {
s += f[i].name;
s += "=".concat(f[i].value);
s = i < f.length-1? s + "&" : s;
}
}

document.getElementById("phone").value = s;

the value should be escaped tho

Fotiman

2:16 pm on Aug 11, 2006 (gmt 0)

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



Don't forget, though, that this input will not get populated if users do not have JavaScript enabled. Thus, you probably shouldn't rely on this for anything other than, say, an Intranet app where you can dictate whether a user has JavaScript enabled or not.