Click here to Skip to main content
15,890,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Whenever i move my mouse on Rss Feed webpart title i am showing hide image, when i click on that image that specific item disabling perfectly.

Here my problem is when i move mouse to any other titles also image showing only on first link .

Please find the my code.

XML
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes"/>
  <xsl:param name="TITLE"/>
  <xsl:template match="rss">
    <script language="javascript" defer="true">
      <![CDATA[
        function showImg(x) {
        document.getElementById('hideimg').style.visibility = 'visible';
        }
        function hideImg(x) {
        document.getElementById('hideimg').style.visibility = 'hidden';
        }
        function hideTitle(x) {
        document.getElementById('divItem').style.display = "none";
        }
    ]]>
    </script>
    <div id="titlediv" style="background:#fff; padding:0; font-size:10px;">
      <xsl:for-each select="channel/item">
        <div id="divItem">
        <a href="{link}" target="_new" onmouseover="showImg(this)" onmouseout="hideImg(this)">
          <xsl:value-of select="title" />
          <img src = "../photogallery/hide1.png" style="visibility:hidden" height="15px" width="15px" id="hideimg" onclick="hideTitle(this);return false;"></img>
        </a>
        <br/>
        <xsl:value-of disable-output-escaping="yes" select="description"/>
        <br/>
        </div>
      </xsl:for-each>
    </div>
  </xsl:template>
  <xsl:template match="description">
    <xsl:value-of select="."/>
    <br/>
  </xsl:template>
</xsl:stylesheet>


Please let me where i did the mistake, Thanks in advance.

Thanks,
Nagendra.
Posted

The id attribute is supposed to be unique across the entire document. You are creating multiple elements with the same ID (hideimg and divItem). When you pass that ID to getElementById, only the first matching element will be returned.

Fixing the image is simple - you can do this with CSS, with no script required.

Fixing the hideTitle function will require navigating the DOM from the passed-in element to the grandparent <div> element. You could use jQuery for this, or the raw javascript solution from this StackOverflow answer[^].
HTML
<style>
<![CDATA[

.rss-feed
{
    background: #fff; 
    padding: 0; 
    font-size:10px;
}
a.rss-title > img
{
    visibility: hidden;
}
a.rss-title:hover > img
{
    visibility: visible;
}

]]>
</style>

<script defer="defer">
<![CDATA[

function closest(elem, selector) {

   var matchesSelector = elem.matches || elem.webkitMatchesSelector || elem.mozMatchesSelector || elem.msMatchesSelector;

    while (elem) {
        if (matchesSelector.bind(elem)(selector)) {
            return elem;
        }
        
        elem = elem.parentElement;
    }
    
    return false;
}

function hideTitle(x) {
    var item = closest(x, ".rss-item");
    if (item) { item.style.display = "none"; }
}

]]>
</script>

<div class="rss-feed">
    <xsl:for-each select="channel/item">
        <div class="rss-item">
            <a href="{link}" target="_new" class="rss-title">
                <xsl:value-of select="title" />
                <img src = "../photogallery/hide1.png" onclick="hideTitle(this);return false;" />
            </a>
            <br />
            <xsl:value-of disable-output-escaping="yes" select="description"/>
            <br />
        </div>
    </xsl:for-each>
</div>
 
Share this answer
 
Comments
Member 8307222 28-Oct-15 3:19am    
Hi Richard,

Thanks for your reply.
Now hide image showing on all the links, its working fine.
But when i click on the image that specific item not hiding, its navigating to link url.

Please let me know where i have to change.

Thanks in advance.
Nagendra.
Richard Deeming 28-Oct-15 9:11am    
Sounds like you're missing the return false; in the onclick attribute.

Either that, or you're getting a script error. Use the browser's developer tools to check the console for script errors.
Member 8307222 29-Oct-15 5:21am    
In onclick function return false; is there.
when i check the error using developer tool, i found one error "Uncaught ReferenceError: hideTitle is not defined". I tried in different ways, but its still navigating to link url.

Richard Deeming 29-Oct-15 9:42am    
So it sounds like the script is not being executed in the global scope.

Try changing:
function hideTitle(x)
to:
window.hideTitle = function(x)
Member 8307222 25-Nov-15 1:37am    
Hi,
changed the function like below

function hide(elements) {
elements = elements.length ? elements : [elements];
for (var index = 0; index < elements.length; index++) {
elements[index].style.display = "none";
}

when i click on image. Image is hiding not item link.
How to hide the item link div(id="itemlink")
Hi Richard,
i am calling hide function in img tag

XML
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="html" indent="yes" />
  <xsl:param name="TITLE" />
  <xsl:template match="rss">
    <style>
      <![CDATA[ 
      .rss-feed
      {
          background: #fff; 
          padding: 0; 
          font-size:10px;
      }
      a.rss-title > img
      {
          visibility: hidden;
      }
      a.rss-title:hover > img
      {
          visibility: visible;
      } 
     ]]>
    </style>
    <script defer="defer">
      <![CDATA[

        function hide(elements) {
            elements = elements.length ? elements : [elements];
            for (var index = 0; index < elements.length; index++) {
              elements[index].style.display = "none";
  }
}
  
]]>
    </script>

    <div class="rss-feed">
      <xsl:for-each select="channel/item">
        <div id="itemlink" class="rss-item">
          <a href="{link}" target="_new" class="rss-title">
            <xsl:value-of select="title" />
            <img src="../photogallery/hide1.png" height="10px" width="10px" onclick="hide(this); return false;" />
          </a>
          <br />
          <xsl:value-of disable-output-escaping="yes" select="description" />
          <br />
        </div>
      </xsl:for-each>
    </div>
  </xsl:template>
  <xsl:template match="description">
    <xsl:value-of select="." />
    <br />
  </xsl:template>
</xsl:stylesheet>


Thanks,
Nagendra.
 
Share this answer
 
Comments
CHill60 25-Nov-15 9:55am    
Richard will not be notified that you are trying to respond to him.
Use the Improve question link to add this extra information to your post then use the "Reply" link next to one of Richard's comments to let him know you have updated the information

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900