Jump to content

get option value


Pain

Recommended Posts

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 by Pain
Link to comment
Share on other sites

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 by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.