user1386906
user1386906

Reputation: 1179

Add argument off function to string in javascript

I want to add an argument of a function to a string (iam not sure if thats the right word)

function changeChart(form, chart, bar) {

            //chart 2 (left)
            $(form).change(function (){

                console.log(form);

                $(form + "option:selected").each(function(){
                //rest of the function

I want the form added to the selection in Jquery.

Is this the right way?

Upvotes: 0

Views: 92

Answers (2)

I Hate Lazy
I Hate Lazy

Reputation: 48789

If form is a string, you need to be sure there's a space before "option:selected".

$(form + " option:selected").each(function(){

If form is an element, pass it to the jQuery functin, then use find:

$(form).find("option:selected").each(function(){

Or use the context parameter (though IMO the code is less clear):

$("option:selected", form).each(function(){

Although, since you're in the handler, you could just use this to reference the form element.

$(this).find("option:selected").each(function(){

Upvotes: 4

Sushanth --
Sushanth --

Reputation: 55750

If form is a string that corresponds to an ID

Then it's supposed to be

$('#' + form)

$('#' + form + " option:selected")

If its a class replace # with a .

Upvotes: 0

Related Questions