/    Sign up×
Community /Pin to ProfileBookmark

unique list of form element names

I need a way to accumulate a unique list of form element names.
I have a solution, but since I am a “part-timer” when it comes to javascripting,
I’d welcome comments or alternatives solutions.
Problem:
As far as a know, the javascript array object does not have a unique() method and adding
one thru prototype enlists probably some sort of nested loop. So here is my idea:
code is untested.

[code]
var names = {} FormName=”myform”,formelement;
var theform = document[FormName]
for(i=0; i<theform.elements.length; i++){
names[theform.elements[i].name] = 1; // arbitrary and boolean true
}
// now by iterating thru names with the in operator we have unique names?
for(var name in names) formelement = theform[name];
[/code]

I repeat, the code is untested, but is my logic sound or is there a better way?
thanks
tim

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERAug 23.2009 — There may be more efficient ways to do this,

but trying to use most of your code

I came up with this:
<i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Element ID values&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/showthread.php?p=1030309#post1030309

var names = {}
var FormName="myForm";
var formElements = [];
var theForm;

function CollectInfo(act) {
names = {};
formElements = [];
theForm = document[FormName];
for(i=0; i&lt;theForm.elements.length; i++){
switch (act) {
case 0 : names[theForm.elements[i].id] = 1; break; // arbitrary and boolean true for 'id'
case 1 : names[theForm.elements[i].name] = 1; break; // arbitrary and boolean true for 'name'
case 2 : names[theForm.elements[i].value] = 1; break; // arbitrary and boolean true for 'name'
}
}
}

// now by iterating thru names with the in operator we have unique names?
function ShowIDS() {
for(var i in names) formElements.push(i);
alert(formElements.join('n'));
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="myForm" name="myForm" onsubmit="return false"&gt;
&lt;input type="text" id="Elem0" name="elem0" value="1"&gt;&lt;br&gt;
&lt;input type="text" id="Elem1" name="elem1" value="2"&gt;&lt;br&gt;
&lt;input type="text" id="Elem2" name="elem2" value="3"&gt;&lt;br&gt;
&lt;input type="text" id="Elem3" name="elem3" value="4"&gt;&lt;br&gt;
&lt;input type="text" id="Elem4" name="elem4" value="5"&gt;&lt;br&gt;

&lt;button onclick="CollectInfo(0)"&gt;Collect IDs&lt;/button&gt;
&lt;button onclick="CollectInfo(1)"&gt;Collect NAMEs&lt;/button&gt;
&lt;button onclick="CollectInfo(2)"&gt;Collect VALUEs&lt;/button&gt;&lt;br&gt;
&lt;button onclick="ShowIDS()"&gt;List Collected Information&lt;/button&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

Good Luck!

?
Copy linkTweet thisAlerts:
@tim042849authorAug 23.2009 — Ah! JMRKER has "built upon" my approach. I like that.

thanks
Copy linkTweet thisAlerts:
@JMRKERAug 23.2009 — You're most welcome.

Glad I was able to help this time.

Good Luck!

?
×

Success!

Help @tim042849 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...