Im work with this script : I want to show/hide text on click, with a single id it works great ( add a fade animation will be great ) but I'm not able to add another ID .. someone could help me ?!
THANKS
function showHide(shID) {
if (document.getElementById(shID)) {
if (document.getElementById(shID+'-show').style.display != 'none') {
document.getElementById(shID+'-show').style.display = 'none';
document.getElementById(shID).style.display = 'block';
}
else {
document.getElementById(shID+'-show').style.display = 'inline';
document.getElementById(shID).style.display = 'none';
}
}
}
/**/
#wrap {
float:left;
width:100%;
margin-top:20px;
max-width: 320px;
padding: 5px;
background-color: #f2f2f2; }
#wrap p{
border-bottom:none;
border-top:none; }
#wrap img{margin: 0 auto; margin-bottom:15px;}
h1 {
font-size: 200%; }
/* This CSS is used for the Show/Hide functionality. */
.more {
display: none;
}
a.showLink, a.hideLink {
text-decoration: none;
background:#fff;
color:#333;
padding: 10px;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
}
a.hideLink {}
a.showLink:hover, a.hideLink:hover {
color:#E99473;
}
<div id="wrap">
<p>
<img src="http://riccardobernucci.com/riviera/images/thumbs/FFBB.jpg" alt=""/>
<a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">+ INFORMAZIONI</a>
</p>
<div id="example" class="more">
<p>Congratulations! You've found the magic hidden text! Clicking the link below will hide this content again.</p>
<p><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>
</div>
</div>
<div id="wrap">
<p>
<img src="http://riccardobernucci.com/riviera/images/thumbs/FFBB.jpg" alt=""/>
<a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">+ INFORMAZIONI</a>
</p>
<div id="example" class="more">
<p>Congratulations! You've found the magic hidden text! Clicking the link below will hide this content again.</p>
<p><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>
</div>
</div>
<div id="wrap">
<p>
<img src="http://riccardobernucci.com/riviera/images/thumbs/FFBB.jpg" alt=""/>
<a href="#" id="example-show" class="showLink" onclick="showHide('example');return false;">+ INFORMAZIONI</a>
</p>
<div id="example" class="more">
<p>Congratulations! You've found the magic hidden text! Clicking the link below will hide this content again.</p>
<p><a href="#" id="example-hide" class="hideLink" onclick="showHide('example');return false;">Hide this content.</a></p>
</div>
</div>
According to HTML specification an
id
property of the element must be unique. You either need to assign different ids to each of your elements, or target them by some less restrictive parameter, such asclass
.However, you're probably better off using jQuery. With it you can do all kinds of selectors and manipulations (including fade effects) and have more reliable and cleaner code.
Here's your code rewritten without any ids: