javascript - Get and set selected option using template -


how change selected option dropdown list using javascript or jquery?

<select id ="someid" name="somename">      <option>one</option>      <option>two</option>      <option>three</option>  </select><br>    <label>item1:</label>  <input type="text" name="item1" value= <%= somevalue %>><br>  <label>line item2:</label>  <input type="color" name="item2" value= <%= somevalue1 %>><br>  <label>item3:</label>  <input type="number" name="item3" min=0 value= <%= somevalue2 %>><br>  <label>item4:</label>  <input type="color" name="item4" value= <%= somevalue3 %>><br>

i had html saved in variable called template. how , set selected option in dropdown list, result same template 1 of 3 options selected. thank in advance. example of result:

<select id ="someid" name="somename">      <option selected>one</option>      <option>two</option>      <option>three</option>  </select><br>    <label>item1:</label>  <input type="text" name="item1" value= <%= somevalue %>><br>  <label>line item2:</label>  <input type="color" name="item2" value= <%= somevalue1 %>><br>  <label>item3:</label>  <input type="number" name="item3" min=0 value= <%= somevalue2 %>><br>  <label>item4:</label>  <input type="color" name="item4" value= <%= somevalue3 %>><br>

there similar javascript way this:

// code setting item_id var template = '<select id ="someid" name="somename"> \     <option '; if(item_id == 1) {     template += 'selected'; } template += ' >one</option> <option '; if(item_id == 2) {     template += 'selected'; } template += ' >two</option> <option '; if(item_id == 3) {     template += 'selected'; } template += ' >three</option> \ </select><br> \ <label>item1:</label> \ <input type="text" name="item1" value= <%= somevalue %>><br> \ <label>line item2:</label> \ <input type="color" name="item2" value= <%= somevalue1 %>><br> \ <label>item3:</label> \ <input type="number" name="item3" min=0 value= <%= somevalue2 %>><br> \ <label>item4:</label> \ <input type="color" name="item4" value= <%= somevalue3 %>><br>'; // code injecting dom 

it's bit more typing , breaking up, solve issue setting template.

if you're it, can utilizing small amount of php instead, won't visible average user (requires serious hacking view active php code if don't have access server). alter template way:

// code setting item_id var template = '<?php $selected = ' + item_id + ' ?> \ <select id ="someid" name="somename"> \     <option <?php if($selected === 1){ echo 'selected'; } ?> >one</option> \     <option <?php if($selected === 2){ echo 'selected'; } ?> >two</option> \     <option <?php if($selected === 3){ echo 'selected'; } ?> >three</option> \ </select><br> \ <label>item1:</label> \ <input type="text" name="item1" value= <%= somevalue %>><br> \ <label>line item2:</label> \ <input type="color" name="item2" value= <%= somevalue1 %>><br> \ <label>item3:</label> \ <input type="number" name="item3" min=0 value= <%= somevalue2 %>><br> \ <label>item4:</label> \ <input type="color" name="item4" value= <%= somevalue3 %>><br>'; // code injecting dom 

the first line of php sets value of variable $selected , next 3 lines check value , set correct option selected. quickest fix know of, requires php processing engine such apache. hosting has enabled (at least i've worked with).


Comments

Popular posts from this blog

matlab - error with cyclic autocorrelation function -

django - (fields.E300) Field defines a relation with model 'AbstractEmailUser' which is either not installed, or is abstract -

c# - What is a good .Net RefEdit control to use with ExcelDna? -