3

I have following id for one of my div id="ftbutton_1357567084/Team:8/User:10/Image:195" i want to replace its html after ajax being called.

When i try to do this using jQuery something like following it doesn't work

jQuery("#"+id).html(SOME HTML HERE);

Where as following code works

document.getElementById(id).innerHTML = SOME HTML HERE;

I know ID should not contain some special characters

ID attributes should be = after an initial letter (a-z or A-Z), may also use 
periods and colons, in addition to letters, numbers, hyphens, and underscores. 

Ref How do I select an element by an ID that has characters used in CSS notation?

But for some reason i can't change the id of each element as it is thought the site.

I also tried following but it doesn't works

function jq( myid ) {
  return "#" + myid.replace( /(:|\.)/g, "\\$1" );
}
jQuery(jq("#"+id)).html(SOME HTML HERE);

I just want to know if this is a case, do i need to use document.getElementById(""+id) instead jQuery("#"+id) ?

OR in other words

Is document.getElementById(""+id) is more reliable than jQuery("#"+id) ?

Salil
  • 46,566
  • 21
  • 122
  • 156
  • 1
    Escape the `/` as well. All characters listed in http://api.jquery.com/category/selectors/. – Felix Kling Jan 10 '13 at 09:39
  • Do a http://jsFiddle.net/ Then you can play around with the id and see if it is your special chars that don't work! – mortb Jan 10 '13 at 09:40
  • similar question: http://stackoverflow.com/questions/9750670/jquery-1-7-1-seemingly-cant-handle-html5-element-ids – Dziad Borowy Jan 10 '13 at 09:41
  • 1
    In your jq() function, you shouldn't return extra '#' as it make double '##' as you are using it – A. Wolff Jan 10 '13 at 09:41

5 Answers5

7

Do this:

$('[id="ftbutton_1357567084/Team:8/User:10/Image:195"]');

It's actually a bit of an interesting question. Most likely jQuery thinks : is initiating a filter, like $('div:visible') and getting gunked up not realizing it's a full ID.

Andy Ray
  • 30,372
  • 14
  • 101
  • 138
2

/ is a meta character, you have to escape it as well:

To use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^`{|}~ ) as a literal part of a name, it must be escaped with with two backslashes: \\.

So this should work fine:

function escape_selector( selector ) {
  return selector.replace( /([!"#$%&'()*+,./:;<=>?@\[\\\]^`{|}~])/g, "\\$1" );
}

(As roasted mentioned in his comment, you need to drop the '#' + as well, otherwise you add it twice to the selector).

DEMO

Community
  • 1
  • 1
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
2

The issue is that you're adding the # twice. Once in the function and once in the actual jQuery call. Should be like this:

function jq( myid ) {
  return "#" + myid.replace( /(:|\.|\/)/g, "\\$1" ); //note that added `/`
}
jQuery(jq(id)).html(SOME HTML HERE); //don't add # again!

A slightly unrelated tip: always remember you can initiate a jQuery instance using an actual DOM object:

jQuery(document.getElementById(someId)); //this will work
Chris
  • 26,544
  • 5
  • 58
  • 71
  • Thanks.....i also check by removing `"#" +` from `function jq( myid )`......but still it doesn't work :( – Salil Jan 10 '13 at 09:54
  • @Salil Notice that you have to escape the `/` as well. See how I edited the Regex in `jq` above. – Chris Jan 10 '13 at 09:54
0

The simplest answer seems to be:

function jq( myid ) {
  return $(document.getElementById(myid));
}

jq(myid).html(...);
A. Wolff
  • 74,033
  • 9
  • 94
  • 155
-4

the correct function is:

function jq( myid ) {
  return *$("#" + myid)*.replace( /(:|\.)/g, "\\$1" );
}
jQuery(jq("#"+id)).html(SOME HTML HERE);
Tim Lang
  • 1
  • 1
  • No, he does not want to select any element inside the function, he wants to modify the string to make it a proper selector. Besides, the code would throw an error because jQuery does not have a `.replace` method. – Felix Kling Jan 10 '13 at 09:45