0

I have a variable which I define by:

  if($(this).hasClass('firstName')){
        lastname = $(this)
            current='.first-name'
        }

I have to use this variable in jQuery selector. I tried the following but it doesn't work:

$('.invitePartners' + current + '[data-index]')
3
  • It should work - $('.invitePartners.first-name[data-index]'), show the whole code and exactly what are you trying to achieve. Commented Jun 18, 2015 at 10:17
  • Shaunak D - I have to use the var itself Commented Jun 18, 2015 at 10:22
  • That is what I have said, your var when appended/concatenated will look like above, which looks valid. Commented Jun 18, 2015 at 10:24

2 Answers 2

1

They way you are trying to do will compile as: $('.invitePartners.first-name[data-index]');

I think, you need spaces between them to find heirarchical DOM elements:

$('.invitePartners ' + current + ' [data-index]');

[Space added after '.invitePartners' and before '[data-index]'], hopefully this will resolve your problem

OR

if($(this).hasClass('firstName')){
        lastname = $(this)
            current=' .first-name ';
        }

add space on both sides of current var value.

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

Comments

0

May be spaces between them as you may want descendant selector.

$('.invitePartners ' + current + ' [data-index]')

Else you will have a value like .invitePartners.first-name[data-index] which will look for an element with class invitePartners and first-name which also have an attribute data-index(something like <div class="invitePartners first-name" data-index="1"></div>)

2 Comments

Arun P Johny- the class of the current supposed to be a child of the .invitePartners , and a parent of [data-index]- $('.invitePartners .className [data-index]')
@SaritSara so use $('.invitePartners ' + current + ' [data-index]')

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.