0

I have a javascript function that takes a value. THe value is begin taken from a dropdownlist. I am unable to pass the value to the function and would like to know what I am doing wrong. Any help would be appreciated.

JS Function

Puma.selectEvent = function(year) {

        var year = year;
        select the event
}

Event on dropdownlist change. The sYear, sMonth, and sSeason are dropdownlist names. The "Puma" is not important.

onChange=Puma.selectEvent(sYear.value);
5
  • Please show us the full code - to which onchange do you add the function? Commented Jul 25, 2012 at 17:10
  • 1
    You've verified that accessing sYear directly, instead of getting by name or id, is working for you? Or you've already retrieved the DOM elements? Commented Jul 25, 2012 at 17:10
  • I'm sorry, I put too many variable. It is the onchange to the sYear dropdownlist. I've tried getElementId, but that didn't work either. Commented Jul 25, 2012 at 17:14
  • If you need to pass the value of the drop down being changed simply have onChange="Puma.selectEvent(this.value);" Commented Jul 25, 2012 at 17:17
  • @ShadowWizard It did not work. Commented Jul 25, 2012 at 17:27

2 Answers 2

1

It looks like you are misunderstanding how event hookups work. Let's start with this line

onChange=Puma.selectEvent(sYear.value);

You probably think that is binding the Puma.selectEvent handler to onChange event ... but you'd be wrong. If you wanted to do that, you'd write:

onChange=Puma.selectEvent;

ie. you'd set the onChange to the "selectEvent" function itself. What you were doing was setting onChange to the output of a call to selectEvent; in other words, what selectEvent returns.

Second, let's look at this line:

Puma.selectEvent = function(year) {

You might think that that will result in you getting the year (from the select) passed in to your function, but you'd be mistaken. ALL event handlers get passed an event object (often called "e"). Now, that event has properties (like target and currentTarget) which you can use to get to the select's value, but you need to do something like e.target.value to do so.

Sign up to request clarification or add additional context in comments.

6 Comments

so how would I go about passing the value to from the dropdown to the function without the parameter? The functions are in two different files.
I'm a little unclear: what element are you binding the onChange to?
@MasterP: If I understood you right, onChange = function() { Puma.selectEvent(sYear.value); } could do the trick.
Ah, thanks rhino I wasn't grokking that that's what OP was looking for. If you want to get fancy, libraries that offer "binding" allow you to pre-set arguments to a function. For instance, using Underscore you can do var selectEventWithYear = _.bind(selectEvent, null, sYear.value); to make a version of selectEvent that always has its first argument set to sYear.value
@rhino I tried that, but the debugging process did not go to the SelectEvent function.
|
0

There's a few problems with the code you posted:

Puma.selectEvent = function (year) {
    // year is a parameter, you should not overwrite it
    var year = year; // get rid of this line
    select the event
}

// this line is EXECUTING Puma.selectEvent(sYear.value)
// and assigning it's return value to the onchange property
onChange=Puma.selectEvent(sYear.value); 

You probably want to do this instead:

onChange = function () {
    return Puma.selectEvent(sYear.value);
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.