/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] document.form.select.options[value].selected=true;?

Hey All,
Trying to run the following in a php foreach loop where $key= form_field_name, and $value = the posted field value, hoping that I can SET the corrisponding options as SELECTED based on the value posted, and then SET remaining text or NON SELECT fields to their posted values in subsequent itterations.

[code=php]
foreach ($arr as $key => $value) {
echo “<script>
// form_name = search_recds field_name = $key
field=document.search_recds.$key;
if(field.tagName==’SELECT’){
document.search_recds.$key.options[‘$value’].selected=true;
//alert(field);
}
else{document.search_recds.$key.value=’$value’;}
</script>”;[/code]

No problem setting the text fields, but can select options only be accessed/set by their numeric array positions?

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@toicontienMar 31.2008 — Yes. You need a function to select an OPTION by value:
function selectByValue(sel, value) {
var i = 0;
var end = sel.options.length;
while(i &lt; end) {
if (sel.options[i].value == value) {
sel.options[i].selected = true;
sel.selectedIndex = i;
return;
}
i++;
}
}

selectByValue( document.search_recds.[I]field_name[/I], "value" );
Copy linkTweet thisAlerts:
@tjmcdauthorMar 31.2008 — FANTASTIC!

Thanks toicontien. That was driving me nuts.

Works like a charm now!!!
×

Success!

Help @tjmcd 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.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

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