Wolf
Wolf

Reputation: 509

Javascript functions in XSLT

This is a part from my XSL:

<input>
  <xsl:attribute name="data-tn">
    RAMPart<xsl:value-of select="../RAMTemplatePartNumber"/>
  </xsl:attribute>
  <xsl:attribute name="data-cn">
    A<xsl:value-of select="Letter"/>
  </xsl:attribute>
  <xsl:attribute name="data-id">
    <xsl:value-of select="../../../RAM/RAMPart1/Id"/>
  </xsl:attribute>
  <xsl:attribute name="value">
    <xsl:value-of select="key('RAMPart1', Letter)" />
  </xsl:attribute>
</input>

I would like to use the JS onchange function, so when I change something in the input field, I will get an alert with the data-tn, data-cn, data-id and the old value. How to do that?

Upvotes: 0

Views: 106

Answers (1)

Tim C
Tim C

Reputation: 70648

Firstly, you should learn about Attribute Value Templates, which may it much easier to write out attribute values. Your XSLT sample could look like this

<input data-tn="RAMPart{../RAMTemplatePartNumber}"
       data-cn="A{Letter}"
       data-id="{../../../RAM/RAMPart1/Id}"
       value="{key('RAMPart1', Letter)}">
</input>

The curly braces indicate an expression to be evaluated, rather than output literally. Using this syntax should make it easier to read, as it is much closer to how the XML gets output.

This should also make it clearer how to do the onchange event:

<input data-tn="RAMPart{../RAMTemplatePartNumber}"
       data-cn="A{Letter}"
       data-id="{../../../RAM/RAMPart1/Id}"
       onchange="alert('data-tn=RAMPart{../RAMTemplatePartNumber}, data-cn=A{Letter}');"
       value="{key('RAMPart1', Letter)}">
</input>

Of course, you will get JavaScript errors, if data-tn or data-cn have apostrophes in, but it should give you the general idea.

Upvotes: 1

Related Questions