user1257724
user1257724

Reputation:

Set different option selected option on refresh of the page -- JAVASCRIPT

so i have a table as follows:

<select id="CellLayout" style="color:#99FF00; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>

on default, 'select layout' is what is displayed. on the click of a button, i need the select box to display 'custom'. i tried searching around SO, but I'm trying to do this without Jquery..

Upvotes: 0

Views: 2776

Answers (4)

rishikesh tadaka
rishikesh tadaka

Reputation: 483

According to my understand of your question. May be following is your answer. Please check.

In html:

<select id="CellLayout" style="color:#99FF00; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>
<input type="button" value="clickHere" onclick="javascript:call()"/>

In javascript:

function call() {
        var textToFind = 'custom';
        var dd = document.getElementById('CellLayout');
        for (var i = 0; i < dd.options.length; i++) {
            if (dd.options[i].text === textToFind) {
                dd.selectedIndex = i;
                break;
            }
        }
    }

Upvotes: 0

adeneo
adeneo

Reputation: 318182

You can just change the selects value, like so :

<select id="CellLayout" style="color:#99FF00; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>

<input type="button" value="change" onclick="change()" />

<script type="text/javascript">
function change() {
    document.getElementById('CellLayout').value = 'custom';
}
</script>

FIDDLE

Upvotes: 2

ѕтƒ
ѕтƒ

Reputation: 3647

Try this... Its simple... Really Works..

<script type="text/javascript">
function fun()
{

document.getElementById("CellLayout").selectedIndex = 4;
}
</script>


<form name="f1">
<select id="CellLayout" style="color:#99FF00; font-family:monospace;" >
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>
<input type="button" onclick="fun()"/>
</form>

Upvotes: 3

jikey
jikey

Reputation: 394

You can try it:

<select id="CellLayout" style="color:#000; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option value="0">select layout</option>
    <option value="1">blinker</option>
    <option value="2">glider</option>
    <option value="3">flower</option>
    <option value="4">custom</option>
</select>
<input id="btn" type="submit" value="setSelected">
<script type="text/javascript">
    var btn = document.getElementById('btn');
    btn.onclick = function(){
        var layOut = document.getElementById('CellLayout');
        for(var i = 0; i < layOut.length; i++){
            if(layOut.options[i].value == '4'){
                layOut.selectedIndex = i;
            }
        }
    }
</script>

Upvotes: 0

Related Questions