Search code examples
jqueryselectimpromptu

Pass selected value to a popup window


I have a window with a select box, which is selected a particular option. In a popup with the same select box values ,i want the select box to select the corresponding select box option from the parent window. I generate the select box fields in the popup via the PHP call, generated directly from the Mysql table. Please solve my doubt

 mystates = [
{
    title: ' list',     
    html:'<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"><tr><td><strong>DATA1</strong></td><td>:</td><td id=\'dataid1\'> <select name=\'dataid1\' id=\'dataid1\' class=\'dataid1\'>'+<?php echo json_encode($options)?>+'</select></td></tr></table>',
    buttons: {  OK: true ,Cancel: -1},
        focus: 1,
        submit:function(e,v,m,f){

        e.preventDefault();
        if(v==-1){
        $.prompt.close();
        return false;
        }
        else if(v){

            dtaafunc(f);
            $.prompt.nextState();
                return false;
            }

        }

},

html is

<?php   
  $result = db_query('SELECT * FROM cart_items' );  
  foreach($result as $wonresRes){
   $options.="<option value='".$wonresRes->ID."'>".$wonresRes->data."     </option>"; 
  }
 ?>
 <table class='mtable'>
<tr>
<td>DATA1</td>
    <td id='dataid1'> 
        <select name='dataid1' id='dataid1' class='dataid1'>
            <?php echo ($options)?>
        </select>
     </td>
   </tr>
  </table>

Solution

  • First of all no two elements will have the same id.Your parent page and the popup contains a selectbox with the same id.Change any one of them.

    var selected = $('#select1').val() //Gives u the current option that is being selected(Parent page)
    

    After the popup is generated use the below to make the selectbox selected

    If you rename ur popup selectbox id to selectbox2

    $('#selectbox2').val(selected) //To be added only after the html is generated
    

    Case 2(Incase of text)

    var selectedTxt = $('#field').text();
    

    To make it selected

    $("#selectbox2 option[value='"+selectedTxt+"']").attr('selected', 'selected'); //based on value
    
    $("#selectbox2 option:contains(" + selectedTxt + ")").attr('selected', 'selected'); //based on text
    

    This will make the popup selectbox selected.Hope that helps mate.. :)