user1652050
user1652050

Reputation: 153

jquery function with parameters not working

I have made a function with parameters but it doesn't work.

This is my code:

 function hideQuestion( _hideQuestion, _showQuestion){
   $('#_showQuestion').removeClass('hideOnInit');
   $('#_hideQuestion').addClass('hideOnInit');
   }

If I use the function like this

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion(question1, question2);' data-role='button'></a>

...nothing happens.

How can can I fix it?

Upvotes: 1

Views: 602

Answers (3)

Adil
Adil

Reputation: 148120

You need to use the function parameter variables and you are using constant string instead. Also pass the ids from onclick as a stirng constant.

function hideQuestion( _hideQuestion, _showQuestion){
   $('#' + _showQuestion).removeClass('hideOnInit');
   $('#' + _hideQuestion).addClass('hideOnInit');
}

Change

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion(question1, question2);' data-role='button'></a>

To

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion("question1", "question2");' data-role='button'></a>

In javascript strings could be enclosed in single or double quotes and could be combined as we did on onclick

 onclick='hideQuestion("question1", "question2");' 

Upvotes: 16

Matt Stokes
Matt Stokes

Reputation: 4958

Adil's solution should work. Your issue is that with your code you are searching for the variables "#_showQuestion" and "#_hideQuestion" which you do not declare or are interested in finding

Upvotes: 0

bipen
bipen

Reputation: 36531

you have a function and a parameter which is fine..but you have not used the parameter in your function... try this.

function hideQuestion( _hideQuestion, _showQuestion){
   $('#'+_showQuestion).removeClass('hideOnInit');  //using the set variable here...
   $('#'+_hideQuestion).addClass('hideOnInit');

}

and your html , yo need to pass the parameter.. here i am sending a string

<a id='_b_Startpage2' href=\"#Question_01_01\" onclick='hideQuestion("question1", "question2");' data-role='button'></a>  // sending thevariable to the functin as parameter

Upvotes: 0

Related Questions