Javascript – Perform JavaScript .replace in JQuery


How would I perform the following in jQuery?

var elmOperator = document.getElementById('Include', 'Operator'));

The ID that is being manipulated would look something like Criteria[0].Include. I am trying to create a variable for a related ID which is Criteria[0].Operator.

Best Solution

Assuming elm is a jQuery object, not a DOM object, you could do this:

var elmOperator = $("#" + elm.attr('id').replace('Include', 'Operator'));

If it is a DOM object, you can do this (which would be a tiny bit faster):

var elmOperator = $("#" +'Include', 'Operator'));

The bigger question is why you'd want to do this. If you don't know something as basic as the jQuery selectors and attr(), is your page using jQuery at all anywhere?