0

I have a h:commandLink in one of the column in datatable.

JSF

<h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"  
rendered="#{routeappcode.edit}"
value="save" onclick="return validateRow(this)"/>

Generated HTML is

 <a id="routeappcodesummary:summarytable:2:save" 
 onclick="var cf = function(){return validateRow(this)};
 var oamSF = function(){return oamSubmitForm('routeappcodesummary','routeappcodesummary:summarytable:2:save');};return (cf()==false)? false : oamSF();" 
    href="#">save</a>

Mojarra 1.2_15

    <a href="#" onclick="var a=function(){return validateRow(this);};var b=function()
{if(typeof jsfcljs == 'function'){jsfcljs(document.getElementById('j_id_jsp_1765393453_2'),
{'j_id_jsp_1765393453_2:j_id_jsp_1765393453_3:0:j_id_jsp_1765393453_7':'j_id_jsp_1765393453
_2:j_id_jsp_1765393453_3:0:j_id_jsp_1765393453_7'},'');}return false};return (a()==false) ?
 false : b();">test</a>

Here the generated javascript for onclick encapsulates the script provided in JSF tag.

function validateRow(link){
    //link is not a link object but some window object.
    var parent = link.parentNode;
}

Here in javascript function we don't get a link object but a window object. Reason is script provided in JSF tag was encapsulated and due to that value of this reference changes.

How can I solve this problem, so that I can get link object in my script?

Using onmouseup won't work in IE 6.

Using JSF 1.2

Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273
changed
  • 2,103
  • 8
  • 36
  • 56
  • I recall issues like that in ancient Mojarra 1.2 versions released about 4-5 years ago, however I don't recognize the autogenerated `oamSF` variable name. What 1.2 impl/version **exactly** are you using? The newer Mojarra ones should log that during webapp startup, otherwise just extract the impl JAR file and read MANIFEST.MF file. If still unsure, just upgrade to latest and retry. [Mojarra](http://javaserverfaces.java.net/) is currently at 1.2_15. – BalusC Apr 15 '11 at 22:20
  • its the same with latest mojarra. – changed Apr 15 '11 at 22:56
  • Google learns me that the generated code is typical for MyFaces. Did you really replace it by Mojarra? If the generated code is the same, then it was definitely not properly upgraded/replaced. – BalusC Apr 16 '11 at 03:40
  • i am using mojarra and tomahawk component library. – changed Apr 16 '11 at 16:19
  • I bet that your classpath is polluted with a mix of Mojarra and MyFaces implementations. The generated JS code in your question's code snippet is typical for MyFaces. I don't recognize those generated JS variable names as from Mojarra. You can not and should not mix JSF implementations. Use the one or the other. I personally recommend Mojarra. Please note that Tomahawk is implementation independent. It's just a component library. Tomahawk doesn't require MyFaces as implementation. It works as good on Mojarra. – BalusC Apr 16 '11 at 16:27
  • For more detail about JSF implementations versus JSF component libraries see also this question: http://stackoverflow.com/questions/2167509/jsf-implementations-and-component-libraries – BalusC Apr 16 '11 at 16:35

2 Answers2

0

Don't know why JSF does that, but here is a workaround for getting the link using jQuery:

  <h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"  
    rendered="#{routeappcode.edit}"value="save" 
    onclick="return validateRow($('a[id*=save]')[0])"/>
0

You can't indeed give your JavaScript function the reference to this, as the onclick code will be encapsulated in a JavaScript function.

You can try to find this link element in your validateRow() function using some JavaScript code, as the one proposed by Stig Henriksen.

Another idea is to add a fake CSS class on your link, and search you element using this class:

<h:commandLink id="save" actionListener="#{ApplicationManagerBean.update}"
    rendered="#{routeappcode.edit}" value="save"
    onclick="return validateRow();" styleClass="saveLink"/>

then, in your JavaScript code (I use jQuery here, but you can use pure JS instead):

function validateRow() {
    // We retrieve a jQuery object:
    var jQueryObject = $("a.saveLink");
    // If you prefer to get a "pure" JavaScript object
    var pureJavaScriptObject = $("a.saveLink").get(0);
    // continue your work here...
}
Community
  • 1
  • 1
Romain Linsolas
  • 79,475
  • 49
  • 202
  • 273