Jquery special character handling for IE
This is a bit embarrassing scenario but I need solution.
I am using some names as an ID for li HTML element. These names are having special characters in it. using JQuery to grab an Id.
lets say my id usage is
var abc = li[id="someCompany=\"Some Term\""]
if I am calling this as
alert($(abc).parent()[0]); or
alert($(abc).html());
I am getting data and everything is working fine in firefox but not in IE.
My actual id display in application is
<li id="someCompany="Some Term""> xyz </li>
I am replacing it with .replace(/"/gi, "\"")
Please let me know, How to get it work in IE, Thanks in advance
Sorry guys, I need to give more information in my question...
Firstly, I get ID as
var aaa = "someCompany="some Data"";
I am replacing it as
aaa = aaa.replace(/"/gi, "\"").replace(/'/gi, "\'").replace(/&/gi, "\&");
Then,
var abc = aaa.replace(/"/gi, "\\\"").replace(/\:/gi, "\:").replace(/'/gi,"\\\'");
and then my question follows.... When I tr开发者_高级运维ied changing the above line to
var abc = aaa.replace(/"/gi, "\"").replace(/\:/gi, "\:").replace(/'/gi,"\'");
everything is fine but I have some names like
var aaa = "someCompany="some's Data""; //In this case I need to handle more special chanracters
Here, .replace(/"/gi, "\\\"")
is working fine in Firefox but not in IE. How can I make it work in IE as well, please suggest
try using browser validation, like single quotes and slash in IE and double in FF.
Which version of IE are you using? I'm on IE9, and on jsFiddle, that works in IE for me (in IE7/8/9 standards mode as well). The only difference I have between your code and mine is that I put quotes around abc, i.e. var abc = 'li[id="someCompany=\"Some Term\""]' ... I'd assume you already had that there though?
http://jsfiddle.net/benno_007/mhHJh/2/
Although .parent()[0] returns an object, not xyz. If you're just needing to access xyz, just use $(abc).html()
Edit:
An alternative to using those awkward IDs could be:
<li id="someCompany" term="Some Term">test</li>
$('#someCompany[term="Some Term"]').html();
Here: http://jsfiddle.net/benno_007/mhHJh/3/
var abc = $("[id='someCompany=\"Some Term\"']");
alert($(abc).html()); //returns xyz
works for me in IE7 and 8, only difference is the single quotes around the whole attribute value instead of the doubles you have
精彩评论