Perform JavaScript .replace in JQuery - Stack Overflow

How would I perform the following in jQuery?var elmOperator = document.getElementById(elm.id.replace(&

How would I perform the following in jQuery?

var elmOperator = document.getElementById(elm.id.replace('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.

How would I perform the following in jQuery?

var elmOperator = document.getElementById(elm.id.replace('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.

Share Improve this question edited Oct 5, 2020 at 14:02 Nanne 64.4k16 gold badges122 silver badges166 bronze badges asked May 12, 2009 at 12:33 BueKoWBueKoW 9665 gold badges18 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

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 = $("#" + elm.id.replace('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?

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 sample we are selecting all the Include elements and then performing a 1:1 mapping of the elements to a new array of operator elements, finally we are setting all the operators background to red.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744710015a4589285.html

相关推荐

  • Perform JavaScript .replace in JQuery - Stack Overflow

    How would I perform the following in jQuery?var elmOperator = document.getElementById(elm.id.replace(&

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信