If i want to click the “.img” class then the div class=”hide pane pane_right” will show off without affecting the others. here’s my code
<ul class="admin-list">
<li>
<!--IOS Start-->
<div class="wrapper">
<div class="pane pane_left" style="display:none;">
<a class="img ios-delete icon-minus" href="#"></a>
</div>
<div class="hide pane pane_right">
<button class="ios-buttons btn btn-danger" href="#">delete</button>
</div>
<div class="pane pane_center">
<div class="pane__content">
<a href="#">[module]_setup_mysql.sql</a>
</div>
</div>
</div>
<!--IOS End-->
</li>
<li>
<!--IOS Start-->
<div class="wrapper">
<div class="pane pane_left" style="display:none;">
<a class="img ios-delete icon-minus" href="#"></a>
</div>
<div class="hide pane pane_right">
<button class="ios-buttons btn btn-danger" href="#">delete</button>
</div>
<div class="pane pane_center">
<div class="pane__content">
<a href="#">labels_en.po</a>
</div>
</div>
</div>
<!--IOS End-->
</li>
<li>
<!--IOS Start-->
<div class="wrapper">
<div class="pane pane_left" style="display:none;">
<a class="img ios-delete icon-minus" href="#"></a>
</div>
<div class="hide pane pane_right">
<button class="ios-buttons btn btn-danger" href="#">delete</button>
</div>
<div class="pane pane_center">
<div class="pane__content">
<a href="#">labels_zh-hans.po</a>
</div>
</div>
</div>
<!--IOS End-->
</li>
<li>
<!--IOS Start-->
<div class="wrapper">
<div class="pane pane_left" style="display:none;">
<a class="img ios-delete icon-minus" href="#"></a>
</div>
<div class="hide pane pane_right">
<button class="ios-buttons btn btn-danger" href="#">delete</button>
</div>
<div class="pane pane_center">
<div class="pane__content">
<a href="#">labels_zhhans.po</a>
</div>
</div>
</div>
<!--IOS End-->
</li>
</ul>
and here’s my jquery
//create an array to store the header id's
tableTitle = new Array();
//iterate through each h2 header element
$('.img').each( function(index) {
//store each h2 id in the array
tableTitle[index] = $(this).attr('id');
});
$(document).ready(function(){
//when user clicks on a header
$(".img").click(
function(){
//create a loop to close all of the paragraphs after user click
for (var i=0; i<3; i++) {
$('#'+tableTitle[i]).find(".pane_right").hide();
}
$(".wrapper").ready(function(){
//check the css of the paragraph associated with the clicked header
if($(this).find(".pane_right").css('display') == 'none'){
//if display is set to none in css, show the paragraph
$(this).find(".pane_right").show();
$(this).width('56%');
}else{
//if the display is not set to none, hide the paragraph
$(this).find(".pane_right").hide();
}
});
}
);
});
Based on your fiddle and you comment, I have a few pointers for ya. First off, this:
Does not really do anything as there are no ID’s asigned to any of the elements containing class
img. In other words you can scratch this previous piece of code all together. Also, quick rule on some basics, anything that is before$(document).ready(function(){is considered “global” and functions like your .each statement wont run in your head this way. It will work in a jsfiddle, but thats because fiddle wraps you js in the doc ready function.Also, in newer jQuery, you can substitute
$(document).ready(function(){ /* do work */ })with$(function(){ /* do work */ })which of course is shorter and easier to remember.Secondly, I notice alot of class use when you need more specifics. Not a problem, I can show you some great jQuery for this without using ID’s. You must understand though, with jQuery selectors, they are just like CSS. a
#is used in front of an elements ID to get that EXACT FIRST MATCH ELEMENT.For example:
On the other hand, a class selector such as
$(".img")will of course grab EVERY SINGLE ELEMENT CONTAINING THAT CLASS, no matter what it might be called from. So in your example, on the.img").click(...(you did correctly btw), the.wrappercall inside is grabbing everything that has the classwrapper. According to your comments, this is undesired.Thus, the rewrite below:
However, if you had wanted to do something after the column was hidden, you can use the toggle’s callback function:
Try each of these in your fiddle, you’ll find they all work, the question is your true intent.
jQuery References: