0

I have a HTML page where many elements are dynamically inserted as the user requests. Each of these elements have an id attribute with some string plus a numeric id value plus another string.

Example: budget_budget_alive_elements_attributes_10_unit_value

And so I tried the following selector:

$("#budget_budget_alive_elements_attributes_\d+_unit_value"); 

But it unfortunately does not work.

Some person wrote an article of an extension to jQuery selector for fetching these kinds of ids but I had no luck with that, either.

Can you help me here? Given the constraints of the element's id values, how can I make a selector for them?

3
  • 2
    You can use Attribute Starts With Selector Commented Jan 6, 2014 at 10:56
  • 2
    I think $('[id^="budget_budget_alive_elements_attributes_"][id$="_unit_value"]'); is good enough, if you still want to validate the digits then use a regex and .filter() Commented Jan 6, 2014 at 10:57
  • thanks @ArunPJohny. Your answer is precisely that! Commented Jan 6, 2014 at 11:00

1 Answer 1

3

You'd better use class selector (by adding same class to these elements).

Else you have to use something like:

$("[id^='idbudget_budget_alive_elements_attributes']") 
Sign up to request clarification or add additional context in comments.

1 Comment

I agree, if you've got groups of elements that do the same thing, classes are the way to go.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.