38

i have an txtBox and its id is : beginDateTxt

but jsf makes it j_idt8:beginDateTxt

in jquery i try to reach it like that

  <script type="text/javascript">
            $(document).ready(function() {
                $(function() {
                    $("#j_idt8:beginDateTxt").mobiscroll().date({
                       theme: 'android-ics light', mode:'scroller', display: 'bottom'
                    });
                });

            });
   </script>

but i get below error:

Uncaught Error: Syntax error, unrecognized expression: unsupported pseudo: beginDateTxt

why?

0

2 Answers 2

65

You could try

$(document.getElementById('j_idt8:beginDateTxt')).mobiscroll().date({theme: 'android-ics light', mode:'scroller', display: 'bottom'});

In general jQuery uses something like CSS selectors in its $() function. In a CSS selector the : denotes a pseudo-class. However, in your case the : is just a part of the id.

If you use the generic getElementById(), the argument is not decomposed, but seen as an ID altogether. So by using getElementById() and wrapping the result with $() you can circumvent this "misunderstanding".

In general, however, I think it would be better to change the namespacing scheme in your JSF.

EDIT

The jQuery documentation on selectors states that you should escape special characters by the use of \\:

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

This will lead to the answer already given by Daniel, which in my opinion is superior to the answer given above. The explanation, however, remains valid.

$("#j_idt8\\:beginDateTxt").mobiscroll().date({theme: 'android-ics light', mode:'scroller', display: 'bottom'});
Sign up to request clarification or add additional context in comments.

4 Comments

I thought we are using jQuery selector so we don't use getElementById
@BobSort The problem here was, that the id included a :, which made jQuery think a pseudo class is used and in the result fail. To circumvent I used getElementById(), which interprets its argument just as a string.
it works. however, I think one of the benefits of using jquery is that we don't have to use getelementbyid. instead of building a jquery node out of the return of d.getelem...() , a real solution would be escaping correctly the string that goes in the jquery selector.
I had completely forgotten about escaping the colon (:). I was using the id verbatim from the code and slowly going mad. thanks for the reminder!
33

If you want to use jQuery id selector you need to escape the : with \ and then to escape the \ (double escape)

Here:

$(function() {
    $("#j_idt8\\:beginDateTxt").mobiscroll().date({
        theme: 'android-ics light',
        mode:'scroller', display: 'bottom'
    });
});

1 Comment

I like this one better. Just answers the question lol.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.