thinklings
asked on
Making a option selected in a select element in my joomla component backend form
Hi, im trying to extend a joomla 3 component's backend form, by adding a datatable which populates data from a table in the database, where the user can add, edit or delete a row. I have the data being displayed and can delete data, but when i come to edit the data im having some issues.
When the user pressed the edit button the data is to be pulled from the database and populated in a form so the user can make changes to it. im getting the data and im populating the form correctly, but i have a select element where i need to make one of the options selected, and i'm having trouble with this, as joomla for some reason alters the select element. below is my code which will make more sense.
// My JQuery code for populating the form
// my form which is located in my components tmpl/edit.php file
// the select element when its rendered in the browser
I have tried many different ways, but im stuck at the moment can any please give me some advice or know what i should be doing.
When the user pressed the edit button the data is to be pulled from the database and populated in a form so the user can make changes to it. im getting the data and im populating the form correctly, but i have a select element where i need to make one of the options selected, and i'm having trouble with this, as joomla for some reason alters the select element. below is my code which will make more sense.
// My JQuery code for populating the form
function populateForm(data) {
var dId = jsl('#ajax-discount #d_id');
var dState = jsl('#ajax-discount #d_state');
var dCode = jsl('#ajax-discount #d_code');
var dType = jsl('#ajax-discount #d_type');
var dValue = jsl('#ajax-discount #d_value');
var formStatus = jsl('#ajax-discount-msg');
dId.val(data.message.id);
dState.val(data.message.state);
dCode.val(data.message.discount_code);
//jsl('select[name="d_type"]').val(data.message.discount_type);
dType.val(data.message.discount_type).trigger( "listz:updated" );
//jsl("#ajax-discount #d_type_chzn > ul.chzn-results > li [value='2']").attr("selected", "selected");
//dType.val(data.message.discount_type);
//dType.change();
dValue.val(data.message.discount_value);
}
// my form which is located in my components tmpl/edit.php file
<div class="control-group">
<div class="control-label">Discount Type</div>
<div class="controls">
<select id="d_type" name="d_type">
<option value="0"></option>
<option value="1">$ AUD</option>
<option value="2">% Discount</option>
<option value="3">Free Shipping</option>
</select>
</div>
</div>
// the select element when its rendered in the browser
<div class="control-group">
<div class="control-label">Discount Type</div>
<div class="controls">
<select id="d_type" name="d_type" class="chzn-done" style="display: none;">
<option value="0"></option>
<option value="1">$ AUD</option>
<option value="2">% Discount</option>
<option value="3">Free Shipping</option>
</select><div class="chzn-container chzn-container-single chzn-container-single-nosearch" style="width: 220px;" title="" id="d_type_chzn"><a class="chzn-single chzn-default" tabindex="-1"><span>Select an option</span><div><b></b></div></a><div class="chzn-drop"><div class="chzn-search"><input type="text" autocomplete="off" readonly=""></div><ul class="chzn-results"><li class="active-result" style="" data-option-array-index="1">$ AUD</li><li class="active-result" style="" data-option-array-index="2">% Discount</li><li class="active-result" style="" data-option-array-index="3">Free Shipping</li></ul></div></div>
</div>
</div>
I have tried many different ways, but im stuck at the moment can any please give me some advice or know what i should be doing.
ASKER
Hi leakim971,
Thanks for the reply, i do know but i cant seem to figure it out correctly, my javascript is a little weak and every time i try to figure it out i confuse my self.
i know i need to get my selector code correct. but other then that i'm hoping a expert can help me.
Thanks for the reply, i do know but i cant seem to figure it out correctly, my javascript is a little weak and every time i try to figure it out i confuse my self.
i know i need to get my selector code correct. but other then that i'm hoping a expert can help me.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Fixed the issue.
1) you know what it the problem.
2) you know how to make an option selected (line 14)
Open in new window
Right?