1

If I have a varibale that changes on certain conditions how do I select it using Jquery selectors?

var id = 2
alert($('.'+id+ ' .title').val());
1
  • 1
    Please clarify the question. What you have looks fine, aside from the fact that the variable is called "id" but you're using a class selector. Commented Dec 1, 2011 at 22:34

1 Answer 1

1

your problem here is that you are starting a classname with a number. class names must start with an _ a - or a letter.

switch up your structure so its:

var id = 2
alert($('.' + 'title' + id).val());

or some variation so that id starts with an underscore, dash, or letter:

var id = 2
alert($('._' + id + ' .title').val());
Sign up to request clarification or add additional context in comments.

1 Comment

The selector in your first example would be ..title2.

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.