Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refresh a select menu in JQUERY MOBILE

I'm creating a web form using JQuery.

I want to copy previous select fields into another select field using a link. i have it changing the DOM, the value of select1.selectedIndex is changed. however it still visually displays the old value. apparently you need to refresh it so it visually updates. I don't understand the syntax of the refresh method i keep reading around. This is how I tried to do it and it doesn't work.

this is the html for the link

    <select name="entry.14.single" id="entry_14">      
      <option value="1"></option>      
      <option value="2">2</option>       
      <option value="3">3</option>       
      <option value="4">4</option>       
      <option value="5">5</option></select> 
    <select name="entry.16.single" id="entry_16">      
      <option value=1""></option>      
      <option value="2">2</option>      
      <option value="3">3</option>       
      <option value="4">4</option>       
      <option value="5">5</option></select> 
    <a href="#" onclick="setSelect('entry_16', 'entry_14');return false;">Same as above</a>

here is my javascript:

    function setSelect(id1, id2){
      var select1 = document.getElementById(id1); 
      var select2 = document.getElementById(id2); 
      select1.selectedIndex = select2.selectedIndex;
      $('#select1').selectmenu('refresh');
    } 

can someone please tell me why my refresh statement is not working?

p.s. i know there are other threads on the same issue, i have also read the 'documentation.' please dont link me somewhere else. I have read other sources and its not getting me anywhere. Please just show me what to change in order to get the refresh working properly.

EDIT: heres a jsfiddle you can mess around on http://jsfiddle.net/AFzqt/7/

like image 412
MoB Avatar asked Apr 13 '12 22:04

MoB


1 Answers

Utilize jQuery event handlers!

The function you are looking for is:

selectmenu("refresh")

In your example, it would look something like this:

$(function() {
    $("#changeButton").on("click", function(e) {
        $("#entry_16").val($("#entry_14").val());
        $("#entry_16").selectmenu("refresh");
        e.preventDefault();
    });
});

http://jsfiddle.net/AFzqt/8/

like image 130
r0m4n Avatar answered Oct 18 '22 23:10

r0m4n