Daan82
Daan82

Reputation: 23

how the use a xsl value as the ID for a onclick

I have the following code: (i use jquery)

<a href="javascript:" 
  onclick="document.getElementById('TEST').style.display = ( (document.getElementById('TEST').style.display == 'block') ? 'none' : 'block' );">
  <xsl:value-of select="$DisplayTitle"/>
</a>

<div id="TEST" style="display:none">
  <xsl:value-of select="@Announcement" />
</div>

This works fine. But I’m pulling multiple elements so the only shows of hide's the first announcement with is logical because that’s the first element with the ID:TEST

Now I would like you use <xsl:value-of select="@ID" /> for the elementbyid but I can't just put that between the quotes. The following is not well formatted

<a href="javascript:" 
  onclick="document.getElementById('<xsl:value-of select="@ID" />').style.display = ( (document.getElementById('TEST').style.display == 'block') ? 'none' : 'block' );">
    <xsl:value-of select="$DisplayTitle"/>
</a>

any tips on how to format this correct?

Upvotes: 2

Views: 4356

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243469

A more compact solution:

<div id="{@ID}">
    <xsl:value-of select="@Announcement"/>
</div>

Also, wirhin the onclick attribute use:

getElementById({@ID})

Upvotes: 1

freefaller
freefaller

Reputation: 19953

You need to use the <xsl:attribute> command within the element you wish the attribute to be placed...

<a href="javascript:">
  <xsl:attribute name="onclick">document.getElementById('<xsl:value-of select="@ID" />').style.display = ( (document.getElementById('TEST').style.display == 'block') ? 'none' : 'block' );</xsl:attribute>
  <xsl:value-of select="$DisplayTitle"/>
</a>

UPDATE based on comments by OP...

As I said above, <xsl:attribute must be WITHIN the element you wish the attribute to be used... in fact it must be the FIRST command afterwards.

Instead of...

<xsl:attribute name="id"><div id="<xsl:value-of select="@ID" />"><xsl:value-of select="@Announcement" /></div></xsl:attribute>

You should have (formatted with newlines for viewing here)...

<div>
  <xsl:attribute name="id"><xsl:value-of select="@ID"/></xsl:attribute>
  <xsl:value-of select="@Announcement"/>
</div>

Upvotes: 0

Related Questions