Can I pass multiple values in the <option> tag?

21,841

Solution 1

You cannot have Multiple values in an Option Tag.

If the Name of the Front and Back-Image only differ by the Page-number, you can use this code:

function selectFlyer(name){
	selectFront(name+"pag1.png");
	selectBack(name+"pag2.png");
}
function selectFront(imgSrc) {
    loadImage(imgSrc);
    var Dim_Slice = document.querySelectorAll(".slice");
    for (var i = 0; i < Dim_Slice.length; i++) {
        Dim_Slice[i].style.backgroundImage = "url(" + imgSrc + ")";
    }
}


function selectBack(imgSrc) {
	loadImage(imgSrc);
    var Dim_Sliceb = document.querySelectorAll(".sliceb");
    for (var i = 0; i < Dim_Sliceb.length; i++) {
        Dim_Sliceb[i].style.backgroundImage = "url(" + imgSrc + ")";
    }
}
<form name="Pictures">
  <select name="dropPic" onchange="selectFlyer(this.value)">
    <option value="Flyer1">Flyer 1</option>
    <option value="Flyer2">Flyer 2</option>
    <option value="Flyer3">Flyer 3</option>
  </select>
</form>

Solution 2

it can be done in this way

<select id="dropPic">
    <option data-picone="Flyer1pag1.png" data-pictwo="Flyer1pag2.png">Flyer 1</option>
    <option data-picone="Flyer2pag1.png" data-pictwo="Flyer1pag2.png">Flyer 2</option>
    <option data-picone="Flyer3pag1.png" data-pictwo="Flyer1pag2.png">Flyer 3</option>
</select>

And on change event you can get the value as below

$("#dropPic").change(function () {
     alert($(this).find(':selected').data('picone'));
}); 

Check this link for similar question

Can an Option in a Select tag carry multiple values?

Solution 3

Your question and / or what you're trying to do isn't entirely clear, but I think this is what you want:

<form name="Pictures">
  <select name="dropPic"
    onchange="selectFront(this.value.split(',')[0]);selectBack(this.value.split(',')[1]);">
    <option value="Flyer1pag1.png,Flyer1pag2.png">Flyer 1</option>
    <option value="Flyer2pag1.png,Flyer1pag2.png">Flyer 2</option>
    <option value="Flyer3pag1.png,Flyer1pag2.png">Flyer 3</option>
  </select>
</form>
Share:
21,841
Pikachuu
Author by

Pikachuu

I only use this site to search for work-related questions.

Updated on August 25, 2021

Comments

  • Pikachuu
    Pikachuu over 2 years

    I have a program that simulates a folding effect on a flyer that has a front image and a back image. I select the flyers through a dropDownList. How can I pass the back image in the tag so that, when I rotate the image, the picture on the back is shown?

    This is the HTML for my dropDownList:

    <form name="Pictures">
            <select name="dropPic" onchange="selectFront(this.value)">
                <option value="Flyer1pag1.png" value="Flyer1pag2.png">Flyer 1</option>
                <option value="Flyer2pag1.png" value="Flyer1pag2.png">Flyer 2</option>
                <option value="Flyer3pag1.png" value="Flyer1pag2.png">Flyer 3</option>
            </select>
        </form>

    Here is the function in JavaScript that changes the front image regarding the selection of the dropDownList and the function that should take the back image of the flyer:

    function selectFront(imgSrc) {
                loadImage(imgSrc);
                var Dim_Slice = document.querySelectorAll(".slice");
                for (var i = 0; i < Dim_Slice.length; i++) {
                    Dim_Slice[i].style.backgroundImage = "url(" + imgSrc + ")";
                }
            }
    
    function selectBack(imgSrc) {
                var Dim_Sliceb = document.querySelectorAll(".sliceb");
                for (var i = 0; i < Dim_Sliceb.length; i++) {
                    Dim_Sliceb[i].style.backgroundImage = "url(" + imgSrc + ")";
                }
            }

  • Lelio Faieta
    Lelio Faieta almost 9 years
    You can use data attributes to store as many other values as you want. Data attributes are there for exactly this kind of needs. You are starting from. Wrong assumption
  • Pikachuu
    Pikachuu almost 9 years
    @TorbenH. That is very good idea. I would never think of that. Thank you! :)
  • Simon.S.A.
    Simon.S.A. about 5 years
    Welcome to stackoverflow. You need to explain your answer - just posting code does not provide a high quality answer.