1

Possible Duplicate:
How to change “selected” value in combobox using JavaScript?

So here's my code"

<label class="conl">Pokaż wyniki jako
<br><select name="show_as">
<option value="topics">Wątki</option>
<option value="posts">Posty</option>
</select>
<br></label>

How to change default selection (from topics to posts)? I can NOT modify this part of HTML code in any way, but I can add a script anywhere inside the head.

Community
  • 1
  • 1
  • If you can use jQuery, then take a look at this: http://stackoverflow.com/questions/496052/jquery-setting-the-selected-value-of-a-select-control-via-its-text-description – frenchie Dec 02 '12 at 13:01

2 Answers2

0

Try it (have not tested):

 var doprdown = document.getElementsByName('show_as')[0];
 for(var i = 0; i < dropdown.options.length; i++){
     if(dropdown.options[i].value == "posts")
          dropdown.selectedIndex = i;
 }

Or jQuery:

$("select[name='show_as'] > option[value=posts]").attr('selected', true);
karaxuna
  • 26,752
  • 13
  • 82
  • 117
0

Try using,

Jquery Code:

<script language="javascript" type="text/javascript" src="js/jquery-1.8.2.js"></script>
<script language="javascript" type="text/javascript"> 
$(function(){
$('#show_as').val('posts');
});
</script>

Html code:

here i have specified an id in-order to select the select box called "show_as"

 <label class="conl">Pokaż wyniki jako
<br><select name="show_as" id="show_as">
<option value="topics">Wątki</option>
<option value="posts">Posty</option>
</select>
<br></label>
Swarne27
  • 5,521
  • 7
  • 26
  • 41