I looked at this tutorial in w3schools for the jQuery toggler slider and wanted to do it for myself. However, my requirement is a little different. I don’t want to hard- code every CSS style and jQuery function .
For example, in the w3school implementation, they used:
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
but I want to make something out of this effect:
function toggleX(fid,pid){
var flipvar = document.getElementbyId(fid);
var panelvar = document.getElementbyId(pid);
$(document).ready(function(){
$(flipvar).click(function(){
$(panelvar).slideToggle("slow");
});
});
}
My requirement is that I should be able to achieve this effect in HTML by simply doing the following:
<div id="flip" onclick="toggleX('flip','panel');">...</div>
<div id="panel">...</div>
<div id="flip1" onclick="toggleX('flip1','panel1');">...</div>
<div id="panel1">...</div>
If it becomes easier for you to see this thing, here is a snippet below. Take a look at this and suggest improvements.
function toggleX(fid,pid){
fid.style.background="#fff";
}
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideToggle("slow");
});
});
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#panel {
padding: 50px;
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<div id="flip" onclick="toggleX('flip','panel');">Only this one works right now(Click me)</div>
<div id="panel">This slider is working only for THIS div and this slider is getting its style from 'panel' and 'flip' CSS. Can I get those CSS attributes by ID?<br/>(for flip1,panel1,flip2,panel2...and so on?)</div>
<div id="flip2" onclick="toggleX('flip2','panel2');">I want to make this DIV slidable show/hide too!</div>
<div id="panel2">And I want to get CSS style from ID too! :/</div>
Run codeHide resultLet me finally break the question again.
I want:
- Slider switch for many different divs
- , CSS
:
, , . , . ID-.
- ... , , , , , ( , ) SO, jsfiddle, . ?
$('.toggler').click(function(evt) {
var $toggler = $(this);
var $container = $toggler.siblings('.container');
$container.toggle();
$toggler.text($container.is(':visible') ? "Hide" : "Show");
evt.preventDefault();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<div class="toggleset">
<a class="toggler" href="#">Hide</a>
<div class="container">
Data to show/hide
</div>
<br/>
<a class="toggler" href="#">Hide</a>
<div class="container">
Data to show/hide
</div>
</div>
Hide result
CSS div, , DIV, (, , 10, , CSS )
- , jQuery , div , , :
( ):
<div id="flip" onclick="toggleX('flip','panel');">...</div>
<div id="panel">...</div>
<div id="flip1" onclick="toggleX('flip1','panel1');">...</div>
<div id="panel1">...</div>
?
- , . , ! ID , , ID .
- : , . - , !
- :. , -, . .