alaeddine.nasri
alaeddine.nasri

Reputation: 182

jQuery : passing concatenated id into selector weired situation

I am using jQuery DatePicker to select elements with '#', everything is working fine when i put "direct" string in the selector, but once i begin concatenating, the result is very suspicious. Maybe this console screenshot explain what i am struggling with enter image description here

HTML :

<input id="wizard:wizard-body:datePiece_picker" name="wizard:wizard-body:datePiece_picker" type="text" value="">

Javascript :

function replaceAll(str, find, replace) {
    return str.replace(new RegExp(find, "g"), replace);
}
var TYPEDIALOG = "wizard:wizard-body:";

var id = replaceAll(TYPEDIALOG, ":", "\\\\:")+ "datePiece_picker";
if ($("#" + id).length != 0) {
    $("#" + id).datepicker({
        dateFormat : "dd/mm/yy"
    });
}

Upvotes: 0

Views: 58

Answers (1)

Jason P
Jason P

Reputation: 27012

Your replace was adding too many backslashes:

http://jsfiddle.net/BaypN/

var id = replaceAll(TYPEDIALOG, ":", "\\:") + "datePiece_picker";

As a side note, this is probably a pretty good argument for avoiding : in element ids. Also, datePiece_picker could be a class instead of part of the id.

Upvotes: 1

Related Questions