www.webdeveloper.com
Results 1 to 11 of 11

Thread: Adding a value to a dropdown list

  1. #1
    Join Date
    Jul 2010
    Posts
    14

    Adding a value to a dropdown list

    I have a code for a dropdown down list:

    <select id="pickone">
    <option id='first_time_user'value ='First Time User'>First Time User</option>
    <option id='frequent_flier'value ='Frequent Flier'>Frequent Flier</option>
    <option id='buying_a_degree' value ='Buying a Degree'>Buying a Degree</option>
    </select>

    Im trying that when i click a submit button, I can add a certain element to each of those options. Does anyone know how?
    I want to display an alert for option[0]
    and for each option[1] and option[2], i want to multiply a total_cost feature by a percentage, 0.75 for option[1] and 0.5 for option[2]

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Hm... When you submit, you change the session, thus any JavaScript (which is a client-side language) previous changes are gone (except if using cookies)

    If you are dealing with a dataBase, probably it would be better to submit the page on itself, and use a server-side language, and probably a session variable, to do that.

    Give us more details.

  3. #3
    Join Date
    Jul 2010
    Posts
    14
    Quote Originally Posted by Kor View Post
    Hm... When you submit, you change the session, thus any JavaScript (which is a client-side language) previous changes are gone (except if using cookies)

    If you are dealing with a dataBase, probably it would be better to submit the page on itself, and use a server-side language, and probably a session variable, to do that.

    Give us more details.
    its client side, im choosing an option from the select box, which either adds an alert, or multiplies a total cost by a percentage (.75, 0.5) and places the new total in a form box.

    if it was a radio button, the code would be:
    if (document.form.radio.checked==true)
    {
    total_cost=total_cost*.75
    }

    im just unsure of how to chnage that statemnt so that it applies to a select box option

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Probably the code should look, schematic, like:
    Code:
    <select id="pickone" onchange="dosomethig(this)">
    and javascript
    Code:
    function dosomething(sel){
    var ind=sel.selectedIndex;
    if(ind==0){
    alert('message here')
    }
    else if(ind==1){
    // multiply that value with 0.75 
    }
    else if(ind==2){
    // multiply that value with 0.5 
    }
    }

  5. #5
    Join Date
    Jul 2010
    Posts
    14
    Quote Originally Posted by Kor View Post
    Probably the code should look, schematic, like:
    Code:
    <select id="pickone" onchange="dosomethig(this)">
    and javascript
    Code:
    function dosomething(sel){
    var ind=sel.selectedIndex;
    if(ind==0){
    alert('message here')
    }
    else if(ind==1){
    // multiply that value with 0.75 
    }
    else if(ind==2){
    // multiply that value with 0.5 
    }
    }

    i already have a function i want it part of, calc(), so if i just change yours:
    function dosomething(sel)

    and

    var ind=sel.selectedIndex

    getting rid of teh function line, since i have one for teh overall function, and then would i change the sel in sel.selectedIndex, to calc?
    or just leave it?

  6. #6
    Join Date
    Jul 2010
    Posts
    14
    i figured out how to do it, thank you very much Kor

    [EDIT] By any chance, would you know how to make that alert appear only the first time it is chosen?
    Last edited by graemeh88; 07-23-2010 at 10:39 AM.

  7. #7
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    The simplest way is probably, to use a global variable:
    Code:
    var myVar=true;
    function dosomething(sel){
    
    var ind=sel.selectedIndex;
    if(ind==0&&myVar){
    alert('message here')
    myVar=false;
    }
    ...

  8. #8
    Join Date
    Jul 2010
    Posts
    14
    Quote Originally Posted by Kor View Post
    The simplest way is probably, to use a global variable:
    Code:
    var myVar=true;
    function dosomething(sel){
    
    var ind=sel.selectedIndex;
    if(ind==0&&myVar){
    alert('message here')
    myVar=false;
    }
    ...
    i tried what you said but it didnt work.
    however i changed the name, that couldnt affect it could it?

  9. #9
    Join Date
    Mar 2010
    Posts
    2,803
    changing the name should not make any difference if it is changed in all the right places and doesn't clash with an existing name elsewhere.

    If you post your current code it will be easier to figure out what is going on.

  10. #10
    Join Date
    Jul 2010
    Posts
    14
    Quote Originally Posted by tirna View Post
    changing the name should not make any difference if it is changed in all the right places and doesn't clash with an existing name elsewhere.

    If you post your current code it will be easier to figure out what is going on.
    i figured it out, but it doesnt make sense to me
    it was based upon an option in the drop list, and as long as it stayed on that option, and i kept pushing submit button, it would show up, but once i went to a different option and back to it, it would stop showing completely like i wanted

  11. #11
    Join Date
    Mar 2010
    Posts
    2,803
    Quote Originally Posted by graemeh88 View Post
    i figured it out, but it doesnt make sense to me
    If it still doesn't make sense, maybe post the code as you currently have it and I or someone else can try to explain.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles