How can I know the id of a JSF component so I can use in Javascript

前端 未结 6 1402
北海茫月
北海茫月 2020-11-22 02:47

Problem: Sometimes you will want to access a component from javascript with getElementById, but id\'s are generated dynamically in JSF, so you

6条回答
  •  Happy的楠姐
    2020-11-22 03:22

    Id is dynamically generated, so you should define names for all parent elements to avoid j_id123-like ids.

    Note that if you use jQuery to select element - than you should use double slash before colon:

        jQuery("my-form-id\\:my-text-input-block\\:my-input-id")
    

    instead of:

        jQuery("my-form-id:my-text-input-block:my-input-id")
    

    In case of Richfaces you can use el expression on jsf page:

        #{rich:element('native-jsf-input-id')} 
    

    to select javascript element, for example:

        #{rich:element('native-jsf-input-id')}.value = "Enter something here";
    

提交回复
热议问题