Jquery select attribute with slash

I am trying to select a jQuery filtered object with an attribute value, which is a unique file name. I cannot escape from slashes when the selector is done with var. I lost 2 hours trying to use several combinations, but I think I have something missing. Thanks in advance for the light.

My HTML:

<table class="table table-condensed" id="CLKPMTable_2"><tbody><tr class="click-row" valuetype="PM" filename="\\server\folder\file1" paymentmethodid="1"><td class="col-md-1"><img width="24" height="24" src="/Content/img/123.png" id="PMURLIMG_"></td><td class="col-md-10" id="PM_FILENAME_">BO OCT</td><td class="col-md-1 text-right"><img width="16" height="16" src="/Content/img/notyet.png" id="PM_STATUS_"></td></tr><tr class="click-row" valuetype="PM" filename="\\server\folder\file2" paymentmethodid="2"><td class="col-md-1"><img width="24" height="24" src="/Content/img/visa.png" id="PMURLIMG_"></td><td class="col-md-10" id="PM_FILENAME_">O SEP</td><td class="col-md-1 text-right"><img width="16" height="16" src="/Content/img/notyet.png" id="PM_STATUS_"></td></tr></tbody></table> 

My Javascript:

 var filename = "\\server\folder\file1"; var selec = $('tr[valueType="PM"][filename="'+filename+'"]'); alert(selec.attr("paymentmethodid")); 

Doesn't work with:

 var filenamescaped = filename.replace('\\','\\\\') var selec = $('tr[valueType="PM"][filename="'+filenamescaped+'"]'); 

Also didn't work with either (from: jQuery's official doc to avoid the characters used in css ):

 function jq( myid ) { return myid.replace( /(:|\.|\[|\]|,|=)/g, "\\$1" ); } var filename = "\\server\folder\file1"; var filenamescaped = jq(filename); 

My violinist to do some tests: jsfiddler

+5
source share
1 answer

The backslash value in this case requires doubling twice to make it work as a valid selector

 var filename = "\\\\\\\\server\\\\folder\\\\file1"; var selec = document.querySelector('tr[valueType="PM"][filename="'+filename+'"]'); alert(selec.getAttribute("paymentmethodid")); 
 <table class="table table-condensed" id="CLKPMTable_2"><tbody><tr class="click-row" valuetype="PM" filename="\\server\folder\file1" paymentmethodid="1"><td class="col-md-1"><img width="24" height="24" src="/Content/img/123.png" id="PMURLIMG_"></td><td class="col-md-10" id="PM_FILENAME_">BO OCT</td><td class="col-md-1 text-right"><img width="16" height="16" src="/Content/img/notyet.png" id="PM_STATUS_"></td></tr><tr class="click-row" valuetype="PM" filename="\\server\folder\file2" paymentmethodid="2"><td class="col-md-1"><img width="24" height="24" src="/Content/img/visa.png" id="PMURLIMG_"></td><td class="col-md-10" id="PM_FILENAME_">O SEP</td><td class="col-md-1 text-right"><img width="16" height="16" src="/Content/img/notyet.png" id="PM_STATUS_"></td></tr></tbody></table> 

Update

In your particular case, the value of filename should already be escaped to replace it to work, as in var filename="\\\\server\\folder\\file1"

Note the global switch in the replace command to replace all occurrences

 var filename = "\\\\server\\folder\\file1"; var filenamescaped = filename.replace(/\\/g,'\\\\\\\\'); var selec = $('tr[valueType="PM"][filename="'+filenamescaped+'"]'); alert(selec.attr("paymentmethodid")); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <table class="table table-condensed" id="CLKPMTable_2"><tbody><tr class="click-row" valuetype="PM" filename="\\\\server\\folder\\file1" paymentmethodid="1"><td class="col-md-1"><img width="24" height="24" src="/Content/img/123.png" id="PMURLIMG_"></td><td class="col-md-10" id="PM_FILENAME_">BO OCT</td><td class="col-md-1 text-right"><img width="16" height="16" src="/Content/img/notyet.png" id="PM_STATUS_"></td></tr><tr class="click-row" valuetype="PM" filename="\\server\folder\file2" paymentmethodid="2"><td class="col-md-1"><img width="24" height="24" src="/Content/img/visa.png" id="PMURLIMG_"></td><td class="col-md-10" id="PM_FILENAME_">O SEP</td><td class="col-md-1 text-right"><img width="16" height="16" src="/Content/img/notyet.png" id="PM_STATUS_"></td></tr></tbody></table> 
+3
source

Source: https://habr.com/ru/post/1259694/


All Articles