/    Sign up×
Community /Pin to ProfileBookmark

Passing a form object to a javascript function

Hi
I want to pass a form to a javascript function. I have the function as follows .. and assing a result to one of the hidden fields in the form (arrSelectedUserIDs in the e.g.)
I would like to know wheather this type of f****ion call is possible
thanks in advance

function getSelectedIDs(form){
var arrUserIDs=new Array();
var arrIndex=0;
for(var x=0;form.length;x++){
if(form.elements[x].name==”chkSelect”){
if(form.elements[x].checked){
arrUserIDs[arrIndex]=form.elements[x].value;
arrIndex++;
// alert(document.UserListForm.elements[x].name + ‘ ‘ +arrUserIDs[x] );

alert(arrUserIDs);
form.arrSelectedUserIDs.value=arrUserIDs;
}
}
}

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceFeb 18.2006 — Sure it is. You can call it this way from the FORM tag itself:

<form ...etc... onsubmit="return getSelectedIDs(this)">

The [b]this[/b] keyword will pass an object reference for the form as an argument to your function. However, this in your code is not correct:

form.length

That should be as follows:

form.elements.length
Copy linkTweet thisAlerts:
@santhoshihrdauthorFeb 18.2006 — Thanks very much

I want to call the function from a submit button .. my form's name is "UserListForm" is it valid

<input type="submit" name="action" value="Disable" onClick="getSelectedIDs(document.UserListForm)">

Santhosh
Copy linkTweet thisAlerts:
@phpnoviceFeb 18.2006 — Sure, that works. You don't even have to reference the form by name:

<input ...etc... onclick="return getSelectedIDs(this.form)">

In this case, the [b]this[/b] keyword creates a reference to the INPUT element. The [b]form[/b] property references the FORM in which this INPUT element is found. This makes for foolproof object references. ?
Copy linkTweet thisAlerts:
@santhoshihrdauthorFeb 20.2006 — thanks

can we rewrite the function getSelectedIDs so as to return an array of ID's and assign it to a hidden variable ...

function getSelectedIDs(frmVariable){

var arrUserIDs=new Array();

var arrIndex=0;

for(var i=0;i<frmVariable.elements.length;i++){

if(frmVariable.elements[i].name=="chkSelect" && frmVariable.elements[i].checked ){

arrUserIDs[arrIndex]=frmVariable.elements[i].value;

arrIndex++;

}

}

return arrUserIDs;

}

Thanks and Regards

Santhosh
Copy linkTweet thisAlerts:
@phpnoviceFeb 20.2006 — <i>
</i>function getSelectedIDs(frmVariable) {
var i, str = "", len = frmVariable.elements.length;
for(i=0; i&lt;len; i++) {
if(frmVariable.elements[i].name=="chkSelect"
&amp;&amp; frmVariable.elements[i].checked) {
str += frmVariable.elements[i].value + "|";
}
}
if(str.length &gt; 0) {
str = str.substr(0,str.length-1);
}
frmVariable.arrSelectedUserIDs.value = str;
return true;
}
×

Success!

Help @santhoshihrd 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.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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...