Link to home
Start Free TrialLog in
Avatar of caf210
caf210Flag for United States of America

asked on

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?




Avatar of MuffyBunny
MuffyBunny
Flag of United States of America image

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.
Avatar of caf210

ASKER

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?

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?
Avatar of caf210

ASKER

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.
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
Avatar of caf210

ASKER

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?
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
   }
}
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.
ASKER CERTIFIED SOLUTION
Avatar of MuffyBunny
MuffyBunny
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of caf210

ASKER

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.
Avatar of caf210

ASKER

Expert's discussion on the best way to approach the problem helped me figure out how to achieve this task.