html - jQuery selected options in conditional statements -


i'm trying display text in div named "msg_box" each selected option. works not live. have refresh see result.

the js is..

$(function () {     switch ($('#my_options :selected').val()) {     case '1':         $('.msg_box').html('you selected option one');         break;     case '2':         $('.msg_box').html('you selected option two');         break;     }  }); 

the div text should appear according selected option..

<div class="msg_box"></div>

and form..

<select id="my_options">     <option value="0">select...</option>     <option value="1">something</option>     <option value="2"> else </option> </select> 

can please share how change html() dynamically?

you'll want use a .change() event handler <select>:

try out: http://jsfiddle.net/mjnc3/2/

$(function() {     $('#my_options').change(function() {         var str = 'you selected option ';         switch (this.value) {         case '1':             str += 'one';             break;         case '2':             str += 'two';             break;         }         $('.msg_box').html(str);     }); }); 


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -