Pain Posted September 10, 2013 Share Posted September 10, 2013 (edited) Hi.I want to get the value (or rather text) of selected option. function val() { var num1 = parseInt(document.getElementById("over_16").value); var mytrip = document.getElementById('privated').text; if(mytrip = 'Lake District') { document.getElementById("sum").value = num1 * 55; } else if(mytrip = 'Private Trip') { document.getElementById("sum").value = num1 * 120; } } Somehow i always get the value which is in the first IF statement. It doesnt matter which one is selected, the value will be the one in the first IF statement. if(mytrip = 'Lake District') { document.getElementById("sum").value = num1 * 55; } and this is my <select> code <select name="trip" style="width: 205px" id="privated"> <option value="Lake District">Lake District</option> <option value="Private Trip">Private Trip</option> </select> this is how the function gets activated (another dropdown list) switch($available_places[0]) { case 1: $select = '<select id="over_16" name="over_16" style="width: 206px" required="required" onchange="val()"><option>0</option><option>1</option></select>'; break; case 0: $select = '<select id="over_16" name="over_16" style="width: 206px" required="required" onchange="val()"><option disabled="disabled">0 places left</option></select>'; break; } what could be the problem here? Edited September 10, 2013 by Pain Quote Link to comment https://forums.phpfreaks.com/topic/282041-get-option-value/ Share on other sites More sharing options...
Pain Posted September 10, 2013 Author Share Posted September 10, 2013 actually if i put an alert into both of those and change from else if to if - i get two alerts with different values :// Quote Link to comment https://forums.phpfreaks.com/topic/282041-get-option-value/#findComment-1448987 Share on other sites More sharing options...
Psycho Posted September 10, 2013 Share Posted September 10, 2013 (edited) The problem is that you are using the assignment operator (a single equal sign) and not the comparison operator (the double equal sign). So your conditions are trying to assign the value to the variable 'mytrip'. Since that operation can be performed the result of the condition is True. Edit: for this you should probably be using a switch anyway as it makes it much easier to edit/maintain. function val() { var count = parseInt(document.getElementById("over_16").value); var mytrip = document.getElementById('privated').text; var cost; switch(mytrip) { case 'Lake District': cost = 55; break; case 'Private Trip': cost = 120; break; default: echo 'Invalid trip selection'; return; } var sum = count * cost; document.getElementById("sum").value = sum; } Edited September 10, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/282041-get-option-value/#findComment-1448993 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.