/    Sign up×
Community /Pin to ProfileBookmark

Highlighting multiple options in a multiselect box

Hi all!

I’m using a multiselect box on my website, similar to this one:

<Select Name=”multiselect[]” multiple=”multiple”>
<option>This is option 1</option>
<option>This is option 2</option>
<option>This is option 3</option>
<option>This is option 4</option>
</select>

Now I want to set option 1, option 2 and option 3 as selected (highlighted) in javascript. How would I go about doing this?

I tried something like this:
document.taakform.elements[‘multiselect[]’].selectedIndex = “1”
document.taakform.elements[‘multiselect[]’].selectedIndex = “2”
document.taakform.elements[‘multiselect[]’].selectedIndex = “3”

Of course this doesn’t work (else I wouldn’t be posting here ?), but I hope you catch my drift.

Thanks in advance guys (and gals)!

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Sterling_IsfineJun 27.2010 — [CODE]box.options[ index ].selected = true;[/CODE]
Copy linkTweet thisAlerts:
@tirnaJun 27.2010 — one way to do it is this:

[code=php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
<!--

window.onload=function() {
document.getElementById("selMyList").options[1].selected = true;
document.getElementById("selMyList").options[3].selected = true;
document.getElementById("selMyList").options[5].selected = true;
}

//-->
</script>

</head>
<body>

<form action="test.php" method="post">
<select name="selMyList[]" id="selMyList" multiple="multiple" />
<option value="val_1">Option 1</option>
<option value="val_2">Option 2</option>
<option value="val_3">Option 3</option>
<option value="val_4">Option 4</option>
<option value="val_5">Option 5</option>
<option value="val_6">Option 6</option>
</select>

<input type="submit" value="submit" />

</form>

</body>
</html>
[/code]


and to check the sent selections on the $erver side php script you could use something like this:

[code=php]
<?php

foreach($_POST['selMyList'] as $value) {
echo $value.'<br />';
}

?>
[/code]
×

Success!

Help @Cannabas 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.17,
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,
)...