Sebastien
Sebastien

Reputation: 6660

Escape dot on jQuery XML select

I have an XML :

<computers>
  <computer>
    <description>Description</description>
    <computer.always></computer.always>
  </computer>
</computers>

So i want to access the computer.always element, so I use this :

$(xml).find('computer ' + "computer.always".replace(/\./g,'\\\\.'))

And the element is not found. ( I wrote "computer.always".replace(/./g,'\\.') because "computer.always" can be a variable and i need to make this selector dynamic. )

The weird thing is :

When I do :

$(xml).find('computer computer\\.always') // Element found

My element is find.

But when I use the result of "computer.always".replace(/./g,'\\.'), it fails.

"computer.always".replace(/\./g,'\\\\.') // return "computer computer\\.always" in the console
 $(xml).find("computer.always".replace(/\./g,'\\\\.')) => []

Anyone can help me?

Upvotes: 1

Views: 194

Answers (2)

darks
darks

Reputation: 58

"computer.always".replace(/./g,'\\.') is not working because you just need "computer\.always".

So if instead you do it like this, it should work.

$(xml).find( "computer " + "computer.always".replace(/./g,'\\.') )

Hope it helps.

Upvotes: 1

Sebastien
Sebastien

Reputation: 6660

In fact, my error was to use double \, so the console use it to echo but not my code. My solution was to use :

$(xml).find("computer.always".replace(/\./g,'\\.'))

Upvotes: 0

Related Questions