Sid Webber
Sid Webber

Reputation: 23

Dynamic Variable Selectors in JQuery

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());

Upvotes: 1

Views: 115

Answers (1)

Evan
Evan

Reputation: 6115

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());

Upvotes: 1

Related Questions