Reputation: 33
I have a visible and a hidden select option. When user selects something, I want to get the selected option and search text in hidden select option. Because I need the val of hidden select option that matches with the selected text. First I tried;
var testx=document.getElementById("priceMatrix");
testx.options[testx.selectedIndex].text=toAdd+"|"+cupSize;
it works, I can set text but still can not set the value. and I tried secondly;
var myText = toAdd+"|"+cupSize;
$('#priceMatrix').filter(function () { return $(this).html() == myText; }).prop('selected', true)
var alertText = $("#priceMatrix option:selected").val();
alert(alertText);
but still it does not work, $("#priceMatrix option:selected").val(); gives the first row's value, not the selected one.
So, what is the problem? is it about button click?
Upvotes: 0
Views: 1295
Reputation: 33
I found a solution and Want to share it. I made a for loop to go into the select. when I find the matching text, I set it selected then I get the value.
var selectedOption = toAdd+"|"+cupSize;
var selectLength = document.getElementById("priceMatrix").length;
var priceMatrix = document.getElementById("priceMatrix");
for(i=0; i<selectLength;i++){
if (priceMatrix[i].text === selectedOption) {
priceMatrix[i].selected = 'selected';
var price = $("#priceMatrix option:selected").val();
var totalPrice = itemNbr1*price;
}
}
alert(totalPrice);
Upvotes: 1