Parameter-Driven Javascript in XSL

As a follow on to my last post, getting the syntax right for Javascript to work well within XSL can drive you a little batty at times.  One good trick is using the <xsl:attribute> tag.  The XSL below is a snippet from the page that I described in my last post.  When the user checks or unchecks the checkbox, I want to commit the changes (I set a bunch of item column values in the setValues Javascript function) and then return to the same page with the same query string arguments.  Getting all of that right in the onclick event parameter is problematic at best, impossible at worst.  By using the <xsl:attribute> tag, I can “step out of” the <input> tag context and let the XSL do its thing.
<input title="RequestTicket" name="RequestTicket" id="ff4{$Pos}" type="checkbox">
  <xsl:attribute name="onclick">
    setValues(this.checked, '<xsl:value-of select="$Pos"/>', '<xsl:value-of select="$RequestID"/>');
    <xsl:value-of select="ddwrt:GenFireServerEvent(concat('__commit;__redirect={',$URL,'?EventID=',$EventID,'&amp;RequestID=',$RequestID,'}'))"/>
 <xsl:if test="@RequestID = $RequestID">
  <xsl:attribute name="checked" />
  • In the setValues line, I can easily pass along the two parameter values ($Pos is the current record number and $RequestID is a parameter that I get from the query string) and, even nicer, I have access to this.checked (the checked state of the checkbox itself).
  • In the ddwrt:GenFireServerEvent line, I can also easily include parameter values ($URL is the Server Variable for the current page’s URL, etc.) and concatenate everything together (using concat).
  • Finally, as I described before, the <xsl:if> section allows me to set the checked state of the checkbox by comparing the RequestID in each item with the value of RequestID passed in on the query string.