Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery selectors and backslashes

I have a dom element that contains a fully qualified name as part of the id attribute;

<div id="domain\element\div">My Div</div>

It seems impossible to get jQuery to select the element by ID. Here's my experiment;

var $e1 = $('#domain\\\element\\\div');
var $e2 = $('#domain\\element\\div');
var $e3 = $(document.getElementById('domain\\\element\\\div'));
console.log($e1);
console.log($e2);
console.log($e3);

The output of the console displays the first two as empty while the third works;

[]
[]
<div id=​"domain\element\div">​TODO write content​</div>​

I am using jQuery 1.5.2. Is this a bug with jQuery itself or are my selector strings wrong?

like image 267
Kevin Avatar asked Apr 28 '11 22:04

Kevin


People also ask

How to use jQuery selectors?

With jQuery selectors, you can find or select HTML elements based on their id, classes, attributes, types and much more from a DOM. In simple words, you can say that selectors are used to select one or more HTML elements using jQuery and once the element is selected then you can perform various operation on that.

Why all jQuery selectors start with a Dollor sign and parenthesis?

All jQuery selectors start with a dollor sign and parenthesis e.g. $ (). It is known as the factory function. Every jQuery selector start with thiis sign $ (). This sign is known as the factory function. It uses the three basic building blocks while selecting an element in a given document. It represents a tag name available in the DOM.

How to select all elements based on name in jQuery?

All selectors in jQuery start with the dollar sign and parentheses: $ (). The jQuery element selector selects elements based on the element name. You can select all <p> elements on a page like this:

What is methodname in jQuery selector?

A jQuery statement typically follows the syntax pattern: $(selector).methodName(); The selector is a string expression that identifies the set of DOM elements that will be collected into a matched set to be operated upon. jQuery selectors return jQuery objects, not DOM objects which are returned from javascript selectors.


1 Answers

If you can change the ID within your HTML code, find some other separator than a backslash \ for your ID so that you can make a valid ID for your selector (see here). An underscore _ would be good.

If you can't alter the HTML, jQuery can still work with backslashes in IDs and ID selectors. Except, you'll need to use four backslashes to match each literal backslash in your ID selector:

$('#domain\\\\element\\\\div')

You achieve this by

  1. Taking the ID:

    domain\element\div
    
  2. Adding the # symbol and escaping the backslashes for the selector:

    #domain\\element\\div
    
  3. Escaping each pair of backslashes for use in JavaScript strings by doubling them (also notice the quotes):

    '#domain\\\\element\\\\div'
    
  4. Then passing the string to $() as above.

jsFiddle

like image 147
BoltClock Avatar answered Sep 28 '22 03:09

BoltClock