Execute JavaScript.replace in JQuery

How do I accomplish the following in JQuery?

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

      

The identifier that is being processed will look like Criteria [0]. Enable. I am trying to create a variable for an associated identifier that is criterion [0]. Operator.

Thanks in advance!

+1


a source to share


2 answers


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

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

      

If it's a DOM object, you can do this (which is slightly faster):



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

      

The more question is why you want to do this. Unless you know something basic like jQuery selectors and attr()

, does your page use jQuery anywhere?

+3


a source


You can still use old javascript in jQuery, so for your sample it would be something like this:

var operators = $('[id*=Include]').map(function() {
    return $('#' + $(this).get(0).id.replace('Include', 'Operator'));
});
operators.css('background', 'red');

      



In this example, we select all Include elements, and then do a 1: 1 element mapping with a new array of operator elements, and finally we set all operational backgrounds to red.

0


a source







All Articles