0

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

First of all, that's a great advice, but this is a very simplified version of my code and sometimes I will need the attributes, so how can I do it inside the attribute?
I am not sure I understand. Perhaps if you expanded your question to show your expected output from your XSLT, that would help. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.