1

I'm still new to jQuery and I ran into a small snag. How come the .click function won't work like this:

var $rowStartDate = $("span[id^=rowStartDate]");
var $rowEndDate = $("span[id^=rowEndDate]");

$($rowStartDate, $rowEndDate).click(function() { 
//Notice the variables in this selector

However, it works when I move the elements out of their variables:

$("span[id^=rowStartDate], span[id^=rowEndDate]").click(function() {

3 Answers 3

5

Second argument of $() is context. So you can use add() instead:

$rowStartDate.add($rowEndDate).click(function() { ... });
Sign up to request clarification or add additional context in comments.

Comments

0

The two are very much different. You need to look at the definition of the jQuery() function. When you say $("span[id^=rowStartDate], span[id^=rowEndDate]").click(function() { you are passing a string to the jQuery() function that then acts upon that string. When you say $($rowStartDate, $rowEndDate).click(function() { you are passing 2 parameters to the function which expects:

jQuery( selector [, context] )
jQuery( element )
jQuery( object )
jQuery( elementArray )
jQuery( jQuery object )
jQuery()

Comments

0

Also you can use class for this.

Here is the Html

<span class='rowdate' id='rowStartDate'></span>
<span class='rowdate' id='rowEndDate'></span>

Here is the javascript

$('.rowdate').click(function(){
   //Maybe you want to know which span is clicked?
   if($(this).attr("id")=="rowStartDate"){
        //rowStartDate clicked function starts here
   }else{
        //rowEndDate clicked function starts here
   }
})

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.