JS Toggle show only clicked one and hide all others - javascript

I just found this helpful JS Toggle..
Is it possible to modify this one? I just want to show the actual selected one and hide all others..
<script type="text/javascript">
function toggle(control){
var elem = document.getElementById(control);
if(elem.style.display == "none"){
elem.style.display = "block";
}else{
elem.style.display = "none";
}
}
</script>
link
<br /><div id="test" style="display: none">text</div>

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<script>
function show(idToShow, classToHide) {
var classList = document.querySelectorAll('.'+classToHide);
var thisElem = document.getElementById(idToShow);
var wasShown = (thisElem.style.display == "block");
var i = -1;
while (node=classList[++i]) {
node.style.display ="none";
};
if(!wasShown){
thisElem.style.display = "block";
}
}
</script>
</head>
<body>
<div onclick="show('a','all');">A
<div class="all" id="a" style="display:none">The stuff to show</div>
</div>
<div onclick="show('b','all');">B
<div class="all" id="b" style="display:none">The stuff to show</div>
</div>
<div onclick="show('c','all');">C
<div class="all" id="c" style="display:none">The stuff to show</div>
</div>
</body>
</html>
It was a fight to get IE 8 support, document.getElementsByClassName was not supported in that version. It seems that document.querySelectorAll does work in IE 8, but not in quirks mode. Sample code fixed to use this and engage standard mode.
Note: My first quick hack of this used a for loop rather than the while:
for (var i=0;i<classList.length;i++) {
classList[i].style.display ="none";
};
Bonus points if you can spot when this will fail (hover mouse to see answer):
if there's an element in the list with the id length (in which case classList.length will refer to that element, not the length of the NodeList.)

Related

why is event.button not working properly with mousedown event in Chrome?

Here's a picture of the error:
I'm trying to get the event.button to do something when someone left clicks on a image, but the event.button is not working for me. I don't know why I'm getting this error. Is it saying that my event parameter is undefined in my Javascript code, or is it saying that buttons is undefined? I don't understand, I could use some help with this question. I set event.buttons equal to x and if x == 1 which means if someone left clicks one of images then something happens.
Here is the javascript code:
function dontShowDots(event) {
var x = event.buttons;
if (x == 1) {
arrayDots[0].style.display = "none";
arrayDots[1].style.display = "none";
}
}
Here's my html code:
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="container">
<div class="empty-squares"><img src="images/greenDot.png" height="50px" width="60px"></div>
<div class="empty-squares"><img src="images/greenDot.png" height="50px" width="60px"></div>
<div id="square1"><img src="images/white_pawn.png" onclick="showDots()" height="75px" width="75px"></div>
</div>
<div id="container1">
<div class="empty-squares"><img src="images/greenDot.png" height="50px" width="50px"></div>
<div class="empty-squares"><img src="images/greenDot.png" height="50px" width="50px"></div>
<div id="square2"><img src="images/white_pawn.png" onmousedown="dontShowDots()" onclick="showDots2()" height="75px" width="75px"></div>
</div>
<script src="interactive.js"></script>
</body>
</html>
css code:
.empty-squares img {
display: none;
}
Try:
onmousedown="dontShowDots(event)"
onmousedown="dontShowDots(event)"
One can access the current event through window.event. Just using event is implicitly accessing window.event.

Hide body until it finishes loading

I created this code so my page would be hidden until it finishes loading. But my code doesn't work as I expected. I expected this to hide the BODY until the OnLoad event was triggered.
However, instead, it just stays hidden.
Any help would be appreciated, if there is maybe another, better method of hiding the BODY until it finishes loading, or what's wrong with this one.
Here's what I've tried so far:
function unveil() {
var thebod = document.getElementById("testbody");
thebod.STYLE = "display: block;"
}
<HTML>
<HEAD>
<TITLE>HELLO</TITLE>
</HEAD>
<BODY ID="testbody" ONLOAD="unveil();" STYLE="display: none;">
<div align="CENTER">
HELLO WORLD!
</div>
</BODY>
</HTML>
The DOMContentLoaded event of the window object can do this. But, don't hide the body, hide a wrapper instead. And, when you set the style, make sure to set the style of a CSS property, not the style object itself.
window.addEventListener("DOMContentLoaded", function(){
document.getElementById("wrapper").style.display = "block";
});
#wrapper { text-align:center; background:#e0e0e0; display:none;}
<HTML>
<HEAD>
<TITLE>HELLO</TITLE>
</HEAD>
<BODY>
<div id="wrapper">
HELLO WORLD!
<!-- The following is only added to create a delay in the
parsing of the document -->
<script>
for(var i = 0; i < 100000000; ++i){ var x = i / 3.14; }
</script>
</div>
</BODY>
</HTML>
You're not setting the elements 'style' correctly:
You can either do:
element.style.display = "block";
Or
element.setAttribute('style', "display: block");
Here is a working example:
function unveil() {
var thebod = document.getElementById("testbody");
thebod.style.display = "block";
}
<HTML>
<HEAD>
<TITLE>HELLO</TITLE>
</HEAD>
<BODY ID="testbody" ONLOAD="unveil();" STYLE="display: none;">
<div align="CENTER">
HELLO WORLD!
</div>
</BODY>
</HTML>
Your issue is here:
thebod.STYLE = "display: block;"
which should read:
thebod.style.display = 'block';
Here is the complete approach (using unobtrusive javascript):
var body = document.getElementsByTagName('body')[0];
function unveil() {
body.style.display = 'block';
}
window.addEventListener('load', unveil, false);
body {
display: none;
}
div {
text-align: center;
}
<div>HELLO WORLD!</div>
<HTML>
<HEAD>
<TITLE>HELLO</TITLE>
</HEAD>
<BODY ID="testbody" onload="testbody.style.display = '';" style="display: none;">
<div align="CENTER">
HELLO WORLD!
</div>
</BODY>
</HTML>

Can't click div

I am working on a website to view your stats from my Minecraft Pocket Edition server, and I am having a problem.
I have one div with the id "tabs-minigames" and in this div there are 4 boxes with icons, which are these "tabs", and this div is currently not shown (in css display none)
Now, I have a image at the top of the website that you should be able to click to show the div "tabs-minigames", and the tabs show, but don't disappear after clicking a second time. I cannot see what I am doing wrong.
I am using this function in jQuery to show and hide the minigames tab:
$(document).ready(function(){
var status = false;
$('#sliderimg').on('click', function(){
if(!status){
$("#tabs-minigames").show();
status = true;
}else{
$("#tabs-minigames").hide();
status = false;
}
});
});
I don't see anything wrong with the function, and here is the HTML
<html>
<head>
<title>
View your stats - Legion PE
</title>
<link rel="stylesheet" type="text/css" href="styles/main.css"></link>
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
var status = false;
$('#sliderimg').on('click', function(){
if(!status){
$("#tabs-minigames").show();
status = true;
}else{
$("#tabs-minigames").hide();
status = false;
}
});
});
</script>
</head>
<body>
<div id="tabs">
<div class="tabs-back">
<div id="tabs-middle">
<img id="sliderimg" src="graphics/slider.png" height="100" width="100" style="position: fixed;padding-left:20px;padding-top: 10px;" style="outline:none;cursor: pointer;"></img>
<div style="padding-left:150px;padding-top:25px;">
<div id="tab-light-box">
Legion PE
</div>
</div>
</div>
</div>
</div>
<div id="tabs-minigames" style="display: none;outline: none;">
<div class="tabs-minigame">
<img src="icons/kitpvp_fullsize.png" width="120" height="110" style="padding-left:15px;padding-top:20px;"></img>
</div>
<div class="tabs-minigame">
<img src="icons/spleef_fullsize.png" width="120" height="120" style="padding-left:15px;padding-top:15px;"></img>
</div>
<div class="tabs-minigame">
<img src="icons/bowandarrow_fullsize.png" width="120" height="120" style="padding-left:15px;padding-top:15px;"></img>
</div>
<div class="tabs-minigame">
<img src="icons/diamond_fullsize.png" width="120" height="120" style="padding-left:15px;padding-top:15px;"></img>
</div>
</div>
<center>
<div id="center">
</div>
</center>
</body>
</html>
You can see the website live here: http://legionpvp.eu/pe/login/stats.html
Thanks!
you don't have any element with id="div", and that's what you're binding your click to... There's an image with another id, did you mean to use
.delegate("#sliderimg", "click", function(){
}
Also as others have pointed out it's best to use jquery.on(), or in your case you can even just use $('#sliderimg').click() unless you plan to add / remove the element with that id to the page.
you can try this with no need for anything to be true or false
$('#sliderimg').on('click',function(){
$('#tabs-minigames').slideToggle(); // you can use fadeToggle() as well
});
Declare the variable in the $(document).ready function
$(document).ready(function () {
var status = false;
$("#sliderimg").on("click", function () {
if (status === false) {
$("#tabs-minigames").show();
status = true;
} else {
$("#tabs-minigames").hide();
status = false;
}
});
});

Displaying text when link is clicked

This is inside my CSS:
div.hide {
display:none;
}
div.show {
color: #66CCFF;
}
This is in my HTML:
16:10
<script language="JavaScript">
function showText(show,hide)
{
document.getElementById(show).className = "show";
document.getElementById(hide).className = "hide";
}
</script>
<a name="16:10" onclick="showText('text1')" href="javascript:void(0);"></a>
<div id="text1" class="hide">This is your monitors aspect ratio.</div>
I'm trying to make the first link display the "This is your monitors aspect ratio." text lower on the page.
Any help is much appreciated.
Pure CSS Answer
Ok, if you just want to append text after you have moved to a position in a page using an anchor tag, you could do it with nothing but CSS similar to the following:
a:target:after{
content: " Test";
background-color: #ccffcc;
}
What this does is appends the text "Test" after the active anchor and colors. Here is an example page with implementation:
<!DOCTYPE html>
<html>
<head>
<title>Link Printer 2</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<style>
a:target:after{
content: " Test";
background-color: #ccffcc;
}
.bigSection{
height: 200px;
}
</style>
</head>
<body>
<div class="bigSection">
<div><a name="first">First</a></div>
<div>To First</div>
<div>To Second</div>
<div>To Third</div>
</div>
<div class="bigSection">
<div><a name="second">Second</a></div>
<div>To First</div>
<div>To Second</div>
<div>To Third</div>
</div>
<div class="bigSection">
<div><a name="third">Third</a></div>
<div>To First</div>
<div>To Second</div>
<div>To Third</div>
</div>
</body>
</html>
Answer using JavaScript
You need to bind an eventListener and prevent it from moving to the next page. Here is a way to do it with JavaScript or CSS. The JavaScript way will actually set the text to whatever you want. The CSS way will hide actually hide the element.
<!DOCTYPE html>
<html>
<head>
<title>Link Printer</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<style>
.hide{
display: none;
}
</style>
<script>
function jsShowText(event) {
var divToManip = document.getElementById("text");
if (divToManip.innerHTML === "") {
divToManip.innerHTML = "Hello";
}
else {
divToManip.innerHTML = "";
}
event.preventDefault();
}
function cssShowText(event) {
var divToManip = document.getElementById("text");
if (divToManip.className === "") {
divToManip.className = "hide";
}
else {
divToManip.className = "";
}
event.preventDefault();
}
function setListeners() {
document.getElementById("jsPrinter").addEventListener("click", jsShowText, false);
document.getElementById("cssPrinter").addEventListener("click", cssShowText, false);
}
window.onload = setListeners;
</script>
</head>
<body>
<div><a id="jsPrinter" href="" onclick="showText();">Click With JavaScript</a></div>
<div><a id="cssPrinter" href="" onclick="showText();">Click With CSS</a></div>
<div id="text">I'm text</div>
</body>
</html>
"showText" must receive an id parameter to be used with the call to "document.getElementById"
Try this, just 1 link that will display the text below after click:
<a name="16:10" onclick="showText('text1')" href="javascript:void(0);">16:10</a>
<script language="JavaScript">
function showText(id)
{
document.getElementById(id).style.display = "block";
}
</script>
<div id="text1" style="display:none;">This is your monitors aspect ratio.</div>
I'm just using style display to hide/show the element. Hope it helps.
just change your css like this:
div.show {
display:block;
color: #66CCFF;
}
Here I am going to provide an example with something that I was working, thank you Alberto Montellano for the example, that gave me an idea, however what was required at the end was something a little different, with the option not to show the data and display it only when I click and make it disappear when click again. In this example I am going to give you two options; you can have a button or a link to trigger the JS function to display and hide the body text, you can choose if you want the button or link that is way I put a comment (optional), both behave as the same, it is up to you which one you want to use.
<!DOCTYPE html>
<html>
<head>
<!-- CSS -->
<style>
#myDIV {
width: 100%;
padding: 50px 0;
text-align: center;
background-color: lightblue;
margin-top:20px;
}
</style>
</head>
<body>
<!-- text before the button or link -->
<p>Click the "PIN" button (or link) to display PIN options:</p>
<!-- The Pin button (optional) -->
<button type="button" onclick="myFunction()">PIN button:</button>
<!-- The Pin link (optional) -->
</br></br></br>
<a onclick="myFunction()" href="javascript:void(0);">PIN link:</a>
<!--Data will display or hide (toggle)-->
<div id="myDIV"style="display:none;">
These are the steps to get your PIN number: Bla bla bla.
</div>
<p><b>Note:</b> The text display when you click the button or link will take space, if you click again will be toggle.</p>
<!-- JS -->
<script>
function myFunction() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
</body>
</html>

Scrolling through div with JavaScript

I am trying to show one div at a time and scroll trough them over and over. I have modified a Fiddle I found and I got it working on fiddle, but for some reason I cant implement a simple test page with the fiddle. It simply does not scroll trough the divs.
Here is the fiddle: http://jsfiddle.net/Twistar/d6nZP/86/
And here is my implemented code:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="styles/styles.css">
<script type="text/javascript" src="includes/headers/jquery.min.js"></script>
<script type="text/javascript">
function go() {
var visibleBox = $('#container .boxes:visible');
visibleBox.hide();
var nextToShow = $(visibleBox).next('.boxes:hidden');
if (nextToShow.length > 0) {
nextToShow.show();
} else {
$('#container .boxes:hidden:first').show();
}
return false;
}​
setInterval(go, 1000);​
</script>
</head>
<body>
<div id="container">
<div class="boxes" style="display:">first box</div>
<div class="boxes" style="display:none;">second box</div>
<div class="boxes" style="display:none;">third box</div>
<div class="boxes" style="display:none;">forth box</div>
</div>
</body>
Can anyone please tell me what i am doing wrong?
I am guessing that you have a working fiddle but on your local test page its not working
your fiddle works because you got selected the default handlers from left handside dropdowns and its not working on your test page because your jquery handler is missing.
the reason is you are missing the document ready handler here:
$(function(){
setInterval(go, 1000);
});
try replacing with this one and see if helps.
I would probably simplyfy and would go with something like this: http://jsbin.com/osepim/1/
$(function() {
// hide all and show first
$(".boxes").hide().first().show();
setInterval(function(){
moveNext();
}, 1000);
});
function moveNext() {
var box = $(".boxes:visible"),
nextBox = box.next();
if(nextBox.length === 0)
nextBox = $(".boxes:first");
//hide all
$(".boxes").hide();
// show next
nextBox.fadeIn();
}
you need to wrap your javascripts code with the ready event $(document).ready() or use the short version $() and that will only excecute your codes when the page is finished loading so your codes should looks something like that
$(function(){
function go() {
var visibleBox = $('#container .boxes:visible');
visibleBox.hide();
var nextToShow = $(visibleBox).next('.boxes:hidden');
if (nextToShow.length > 0) {
nextToShow.show();
} else {
$('#container .boxes:hidden:first').show();
}
return false;
}​
setInterval(go, 1000);​
});
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="styles/styles.css" />
<style type="text/css">.boxes{display:none}</style>
</head>
<body>
<div id="container">
<div class="boxes">first box</div>
<div class="boxes">second box</div>
<div class="boxes">third box</div>
<div class="boxes">forth box</div>
</div>
<script type="text/javascript" charset="utf-8" src="//ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/javascript">
function go() {
var visibleBox = $('#container .boxes:visible'); // GET THE DIV
visibleBox.hide();
var nextToShow = $(visibleBox).next('.boxes:hidden');
if (nextToShow.length > 0) { // SHOW NEXT ITEM
nextToShow.show();
} else {
$('#container .boxes:hidden:first').show();
}
return false;
}​
setInterval(go, 1000);​ // MS SECOND OF LOOP
</script>
</body>
</html>

Categories

Resources