Javascript Jump Menu With Multiple Arguments/Parameters

Let's say I have a function that uses to parameters, galleryText and galleryFolder, like so:

function changeGallery(galleryText,galleryFolder) {
      
      // my Function

}


I might run it like this:



changeGallery("The Name of My Gallery","images/foldername/");



But I wanted to run it through a jump menu, where each of the options has their own value for the parameters. So I've got this in my HTML:

<form name="form" id="form">
     <select name="jumpMenu" id="jumpMenu" onchange="changeGallery(?????)">

       <option selected="selected">Choose the Gallery of Your Choice</option>
       <option value="???">Option 1</option>
       <option value="???">Option 2</option>
     
     </select>
   </form>

But I'm not sure how to set up the "?????" in the onchange handler or in the value of the options.

Can anyone help?




caf210Asked:
Who is Participating?
 
MuffyBunnyCommented:
Here's the whole deal:

<script>
function changeGallery(sel){
      if(sel.options[sel.selectedIndex].value == 'g1'){
            document.getElementById("gallery1").style.display='';
            document.getElementById("gallery2").style.display='none';
      }
      if(sel.options[sel.selectedIndex].value == 'g2'){
            document.getElementById("gallery2").style.display='';
            document.getElementById("gallery1").style.display='none';
      }
}
</script>

<form name="myForm">
<select name="mySelect" onchange="changeGallery(this);">
<option value="">Select a Gallery</option>
<option value="g1">Gallery 1</option>
<option value="g2">Gallery 2</option>
</select>
</form>

<div id="gallery1" style="display:none;">
HTML for gallery 1
</div>
<div id="gallery2" style="display:none;">
HTML for gallery 2
</div>


Copy and paste that code into a file and look at it in a browser. I think it's what you're looking for.
0
 
MuffyBunnyCommented:
I wouldn't set the function up to accept the text and the folder.

Set the function up to accept the form and select objects:
function changeGallery(frm, sel) { //function code }

Then in your select definition pass them:
<select name="jumpMenu" id="jumpMenu" onchange="changeGallery(this.form, this)">

If it's the only form that will use the function, you could always pass nothing at all and just refer to the form wholly in your script:
document.form (please don't name your form "form" though... it's well, bad form - sorry about the pun, I couldn't control myself)

Then in your function determine the selected value and do what you need to do with it.
0
 
caf210Author Commented:
So what would my value be in this line?

 <option value="???">Option 1</option>



Ultimately the function needs to be provided a short string of text and the name of a folder, both of which are unique to Option 1 (in the example above).


When we do onchange="changeGallery(this.form, this)", what would the values of frm and sel be that are received in the changeGallery function?

0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
MuffyBunnyCommented:
You could set it to anything that will be useful to you within the function code.

Is the purpose of this drop-down to dynamically change content on your page? Or do you need to actually pass this value to another page? Or both?
0
 
caf210Author Commented:
It's to dynamically change content on the page. (No need to pass it to another page.)

Where I'm not clear, because of my cursory knowledge of javascript, is what "this.form" and "this" represent when the changeGallery function is run.
0
 
MuffyBunnyCommented:
this.form is shorthand for the current form

So if you have:

<form name="myForm">
<select name="mySelect" onchange="changeGallery(this.form, this);">
<option value="1">Gallery 1</option>
<option value="2">Gallery 2</option>
</select>
</form>

In this context, this.form refers to the containing form, myForm, and this refers to whatever object is using the term, in this case, mySelect.

It's just shorter than typing out document.myForm and document.myForm.mySelect
0
 
caf210Author Commented:
So I'm assuming, if someone selected Gallery One, changeGallery wouuld receive "1" passed in as the value of "this." True?

If that is the case, that's one parameter that I need. How would I get the other parameter associated with Gallery 1 into the function?
0
 
MuffyBunnyCommented:
Nope, it's not quite that simple. this.form passes the form object. this passes the select object.

Within your function code, you'll refer to the actual value like this:

function changeGallery(fm, sel){
   if(sel.options[sel.selectedIndex].value == '1'){
       //whatever you want to do with gallery 1
   }else{
      //whatever you want to do with gallery 2
   }
}
0
 
MuffyBunnyCommented:
keep in mind that this type of javascript activity will not reload your page. you'll have to have all versions of the gallery already built in the html and then use the javascript to hide and display the various galleries. That involves using CSS and <div> tags wrapped around your galleries.
0
 
caf210Author Commented:
OK, I got it. I used this concept, but instead of doing the login inside, I just split the string. Here's my final concept:

<form name="myForm" id="myForm">
     <select name="jumpMenu" id="jumpMenu" onchange="changeGallery(this)">
       <option value="choose" selected="selected">Choose the Gallery of Your Choice</option>
       <option value="This is my text;folder1">Gallery 1</option>
       <option value="This is my other text;folder2">Gallery 2</option>
     
     </select>
   </form>



function changeGallery(selection) {
      
            
      var textFolder = selection.options[selection.selectedIndex].value;
      
      if (textFolder != "choose") { //if it's not the first option that says "please choose", do this...
                  var splitString = textFolder.split(";") //split the value at the semi-colon
                  var galleryText = splitString[0]; //first half
                  var galleryFolder = splitString[1]; //second half


                    // now I can have both values, and use them in the rest of my function.
      
      }
}


Thanks for all your help. I'll award out the points.
0
 
caf210Author Commented:
Expert's discussion on the best way to approach the problem helped me figure out how to achieve this task.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.