Preethi Jha Preethi Jha - 4 months ago 7
jQuery Question

JQuery after using $.publish to load a select,want to select particular option in struts 2 select tag

I am using

$.publish
to populate the dropdown (Struts 2 select tag) after that, I want the dropdown to be auto-selected to a value.

I tried the following:

$.publish("reloadAttributeList");
//$('#selectedAttribute option[value="attributeId"]');
$('#selectedAttribute').val(attributeId).change();
//$('#selectedAttribute').val(attributeId);


None worked.

Answer

No, after using $.publish() you can't auto select the drop down. The code to auto select dropdown should be inside the $.subscribe(). Some another event you should publish when loading is complete.

<script>
  $.subscribe('loadCompleteAttributeList', function(event, data) {
    $('#selectedAttribute').val(attributeId);
  });
</script>