1

I have a drop down list

<div class="col-lg-8">
   <select class="form-control" id="question_type" name="agreement[question_type]">
      <option value="">Select Type</option>
      <option value="ss">Single select (checkbox)</option>
      <option value="ms">Multi option (radio)</option>
   </select>
</div>

After submitting the form I have placed this datas in table ,

<tr data-view-key="<?php echo $jagr_id; ?>">
   <td class="question_type">
   <?php
   if ($jagr_question_type == 'ss') {
       echo 'Single select (checkbox)';
   } else if ($jagr_question_type == 'ms') {
       echo 'Multi option (radio)';
   }
   ?>
    </td>
    <td><a href="#<?php echo $jagr_id; ?>" name="edit_agreement" ><span class="glyphicon glyphicon-edit" title="Edit"></span></a>&nbsp;&nbsp; <a href="#<?php echo $jagr_id; ?>" name="delete_agreement" ><span class="glyphicon glyphicon-trash" title="Delete"></span></a></td>
</tr>

and on the edit button click,i want to display the selected item in drop down list. following code is used for edit click

$(document).on("click", "a[name = 'edit_agreement']", function(event)
{
   event.preventDefault();
   var jagr_id = $(this).attr("href").substring(1);
   var parent = $(this).parents("[data-view-key='"+jagr_id+"']");
   var question_type = parent.find("td.question_type").text();
   $("#hdn_jagr_id").val(jagr_id);
   $("#question_type option:selected").text(question_type);     
});

using $("#question_type option:selected").text(question_type); i can display the text in the drop down list, but again submit the form the value not posted, Now the html part changed like this(repeating th eselected item)

<div class="col-lg-8">
   <select class="form-control" id="question_type" name="agreement[question_type]">
      <option value>Single select (checkbox)//selected item</option>
      <option value="ss">Single select (checkbox)</option>
      <option value="ms">Multi option (radio)</option>
   </select>
</div>
1
  • @Guruprasad question_type is not an id,fetched the data from the table using ("td.question_type") Commented Dec 30, 2015 at 4:28

2 Answers 2

1

I really don't know what the question_type should be .. is it a text of option or value of option .. in all ways you need

.prop('selected' , true);

if question_type is a value of option

$("#question_type option[value='"+question_type+"']").prop('selected' , true);  

if question_type is a text of option you can use .filter();

$("#question_type option").filter(function(){
   return $(this).text().trim() == question_type;
}).prop('selected' , true);  

Working Demo

Sign up to request clarification or add additional context in comments.

5 Comments

var question_type = parent.find("td.question_type").text(); console.log( question_type); //output- Single select (checkbox)(selected item)
'question_type' is text
@Ann Ok Ann I updated my answer if text and with simple demo as well
filter function not working,I have tried this code,the selected item not displayed in the list.
0

This option worked for me:

$(document).ready(function(){

   $("#countryList").change(function(){

       var selectedCountry = $(this).children("option:selected").val();

       console.log("You have selected the country - " + selectedCountry);

   });

});

Of course, you need to have HTML list of countries. Something like this:

<select id="countryList">
    <option value="1">USA</option>
    <option value="2">Brasil</option>
    <option value="3">Spain</option>
</select>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.