JQuery - add onclick to TR

I use Richfaces and one of rich: dataTable generates this html:

<form id="scheduleForm">
  <table id="scheduleForm:rowList">
    <tr>
      <td></td>
    </tr>
    <tr>
      <td></td>
    </tr>
    <tr>
      <td></td>
    </tr>
  </table>
</form>

I want to act by clicking on TR for this table. How can i do this? The following also works for other tables on the page, and I get alert () for other rows, and I don't want this.

        jQuery('#scheduleForm:rowList tr').click(function(event) {
            alert(1);
        });

O ':' char. It is created by Richfaces.

Source:

<h:form id="scheduleForm">
<rich:dataTable id="rowList">
</rich:dataTable>
</h:form>
+3
source share
7 answers

I know that :JSF / RichFaces is being created. I used to have problems with :. This is probably not what jQuery people in ID expected. They use it for selectors. What you can do is:

jQuery('table[id = "scheduleForm:rowList"] tr').click(...)

Check out http://jee-bpel-soa.blogspot.com/2009/04/tips-and-tricks-on-jquery-and-richfaces.html

+2
source

. .

jQuery(document.getElementById('scheduleForm:rowList').getElementsByTagName('tr')).click(function(event) {
          alert(1);
        });
+1

, tr id.

<form id="scheduleForm">
  <table id="scheduleForm:rowList">
    <tr id="tr1">
      <td></td>
    </tr>
  </table>
</form>

jQuery('#tr1').click(function(event) {
    alert(1);
});
0

:

jQuery('#scheduleForm:rowList>tr').click(function(event) {
            alert(1);
        });

":" id. < , .

0
source

You have a colon :in the ID attribute. Colon is of particular importance in jQuery selectors, so you need to get away from it like this:

jQuery('#scheduleForm\\:rowList tr').click(function(event) {
      alert(1);
});
0
source

The: generated by JSF. You can also set prependId = "false" in the form. Then just use:

jQuery('#rowList tr').click(function(event) { alert('hello'); });

and only this table will be selected.

0
source
jQuery("#rowList").on("click", "tr", function(event) { 
    alert($(this).id); 
});
0
source

Source: https://habr.com/ru/post/1783852/


All Articles