tessa tessa - 1 year ago 97
jQuery Question

How do you escape parentheses in jQuery selector?

I am trying to check if a td innertext contains parentheses (). The reason is I display negative numbers as (1000) and I need to convert them to -1000 to do math. I've tried a couple different ways but can't seem to get it right. I know there are non-jQuery ways to do this but at this point it's just bugging me.

$(tdElement[i]).find("\\(").length > 0

This doesn't throw error, but it doesn't find an innertext of (1000):

context: {object}
jquery: "1.3.1"
length: 0
prevObject: {...}
selector: "\("

Another method I tried was:

$("#fscaTotals td").filter(":contains('\\(')")

This throws error "Exception thrown and not caught". It seems to work for other characters though. Example: . , ; < >

So, how do you escape parentheses in jQuery?

Answer Source

I think you'll have to use a filter function like:

$('#fscaTotals td *').filter(function(i, el) {
    return !!$(el).text().match(/\(/);

Edit: I think this is a bug in jQuery's :contains().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download