www.webdeveloper.com
Results 1 to 9 of 9

Thread: multi value to store from a select field

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    5

    Question multi value to store from a select field

    Hi, so since two days I have a problem with my select field.

    I have two values I want to use from this input, if it was possible, I would like to do this :

    <SELECT NAME="flats">
    <OPTION VALUE="74" value="1"> One
    <OPTION VALUE="106" value="2"> Two
    <OPTION VALUE="141" value="3"> Three
    </SELECT>

    i have tryed to do things like this :

    <OPTION VALUE="4" id="four"> four

    but when I try to get the value from $data = document.form.selectfield.id;
    It does not work, the syntax seems to be incorrect.

    I have tried to do an onclick to set the value on an hidden field like this :

    <OPTION VALUE="42" onclick = "document.form.hidden5.value = 7;"> five

    But onclick does not work on select fields :/

    So, what is my goal ?
    I have on my site a list (so a select field) of sizes of flat (1 room to 10 rooms), each line does have a value i need for some maths, but I need to save which choice the
    user done (because some rooms have the same value)

    I would be really grateful if someone can helps me :')

  2. #2
    Join Date
    Mar 2009
    Posts
    452
    you can get the value easily like this

    <SELECT NAME="flats" onchange="alert(this.value);">
    <OPTION VALUE="74" value="1"> One
    <OPTION VALUE="106" value="2"> Two
    <OPTION VALUE="141" value="3"> Three </OPTION>
    </SELECT>

    or in javscript

    var flats=flats.value;

  3. #3
    Join Date
    Oct 2012
    Posts
    5
    yeah, that's the way to store one value, what about the second one ? the VALUE = '74' one ?

  4. #4
    Join Date
    Mar 2011
    Posts
    1,133
    You can't have two attributes with the same name in a single element, even if one is upper case and the second is lower case. To browsers, these are identical and one will be ignored. I'd suggest that you find a different way to do whatever you're trying to do here.
    Rick Trethewey
    Rainbo Design

  5. #5
    Join Date
    Oct 2012
    Posts
    5
    I know I can't , that's why I said "I would like" it was possible to do it this way, and I exlain what I have also tryed in 1st post.

  6. #6
    Join Date
    Mar 2011
    Posts
    1,133
    You can use custom attribute names. These are valid in HTML5 and JavaScript will access them even if you use an HTML4 <!DOCTYPE>. Another approach would be to simply store the corresponding custom values in a JavaScript array and not embed them in your HTML at all. There are probably other ways to handle this, too, but its difficult to suggest anything without knowing more about what you're doing.
    Rick Trethewey
    Rainbo Design

  7. #7
    Join Date
    Mar 2009
    Posts
    452
    you can have multiple values separated by commas or etc... then you can split those values.

  8. #8
    Join Date
    Oct 2012
    Posts
    5
    Can you please show me an example ?

  9. #9
    Join Date
    Mar 2009
    Posts
    452
    <SELECT id="flats">
    <OPTION VALUE="74,1"> One </OPTION>
    <OPTION VALUE="106,2"> Two </OPTION>
    <OPTION VALUE="141,3"> Three </OPTION>
    </SELECT>
    then split like this

    flats=document.getElementById('flats').value.split(',');
    alert('First value='+flats[0]);
    alert('Second value='+flats[1]);

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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