Is there a way I can make the on the right appear one at a time? And show by sliding?
Here's the code I'm using to call the
<button onClick="div1()">Try it</button>
<button onClick="div2()">Try it</button>
<button onClick="div3()">Try it</button>
Here is the script I am using
<script>
function div1() {
var x = document.getElementById('myDIV');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
function div2() {
var x = document.getElementById('myDIV2');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
function div3() {
var x = document.getElementById('myDIV3');
if (x.style.display === 'none') {
x.style.display = 'block';
} else {
x.style.display = 'none';
}
}
</script>
I want to make my site look like this
By creating a function where you send the div number ( 1 , 2 , 3 ...) you can slide the concerned div and hide all the others .
Try the bellow snippet : ( using jquery ) ,
function toggleDiv(divNum) {
$("#close").hide();
$(".slide").animate({right:'-200'},350);
if($("#div"+divNum)) {
$("#div"+divNum).animate({right:'0'},350,function(){$("#close").show();});
}
}
$(document).ready(function(){
$("#close").on("click",function(e){
$(".slide").animate({right:'-200'},350);
$(this).hide()
})
})
.slide {
width:200px;
height:100%;
position:absolute;
right:-200px;
top:0;
background:#d2d2d2;
}
#close {
position:absolute;
right:10px;
top:10px;
z-index:10;
display:none;
}
#right-content {
position:absolute;
right:0;
top:0;
overflow:hidden;
width:200px;
height:100%;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onClick="toggleDiv(1)">Try it 1</button>
<button onClick="toggleDiv(2)">Try it 2</button>
<button onClick="toggleDiv(3)">Try it 3</button>
<div id="right-content">
<div id="close">X</div>
<div class="slide" id="div1">content 1</div>
<div class="slide" id="div2">hey I'm content 2</div>
<div class="slide" id="div3">Now it's content 3</div>
<div>
Do like this:
<div id="rightSideDiv" ></div>
<input type="hidden" id="myHiddenField1" value="<b> Info of div 1</b>" />
<input type="hidden" id="myHiddenField2" value="<b> Info of div 2</b>" />
<input type="hidden" id="myHiddenField3" value="<b> Info of div 3</b>" />
<button onClick="doDisplay('myHiddenField1')">Try it</button>
<button onClick="doDisplay('myHiddenField2')">Try it</button>
<button onClick="doDisplay('myHiddenField3')">Try it</button>
<script>
function doDisplay(hiddenFileldID) {
var x = document.getElementById(hiddenFileldID).value;
document.getElementById("rightSideDiv").innerHTML = x;
}
</script>
Related
I'm using switch visible to switch between 3 visible elements on button press, but am only able to get 2. Thanks for the help :)
function switchVisible() {
if (document.getElementById('text1')) {
if (document.getElementById('text1').style.display == 'none') {
document.getElementById('text1').style.display = 'block';
document.getElementById('text2').style.display = 'none';
document.getElementById('text3').style.display = 'block';
}
else {
document.getElementById('text1').style.display = 'none';
document.getElementById('text2').style.display = 'block';
document.getElementById('text3').style.display = 'none';
}
}
}
#text1, #text2, #text3 {
display: none;
}
<div id="text1">text 1</div>
<div id="text2">text 2</div>
<div id="text3">text 3</div>
<input id="Button1" type="button" value="Click" onclick="switchVisible();"/>
You can use the below approach to achieve this. Use a switch statment and a count vairable to keep track of 3 divs.
Full working code snippet:
let count = 1;
function switchVisible() {
switch (count) {
case 1:
document.getElementById('text1').style.display = 'block';
document.getElementById('text2').style.display = 'none';
document.getElementById('text3').style.display = 'none';
count++;
break;
case 2:
document.getElementById('text1').style.display = 'none';
document.getElementById('text2').style.display = 'block';
document.getElementById('text3').style.display = 'none';
count++
break;
case 3:
document.getElementById('text1').style.display = 'none';
document.getElementById('text2').style.display = 'none';
document.getElementById('text3').style.display = 'block';
count = 1
break;
default:
count = 1
}
}
#text1,
#text2,
#text3 {
display: none;
}
<div id="text1">text 1</div>
<div id="text2">text 2</div>
<div id="text3">text 3</div>
<input id="Button1" type="button" value="Click" onclick="switchVisible();" />
Hope that's how you wanted it work.
to do this one proposal can be :
have an array of all div id to switch
have a rolling index that say which div should be displayed
var divIds = [
'text1',
'text2',
'text3'
];
var visibleIndex = 0;
function switchVisible() {
divIds.forEach(id => {
document.getElementById(id).style.display = 'none';
});
document.getElementById(divIds[visibleIndex]).style.display = 'block';
visibleIndex++;
if (visibleIndex === divIds.length) {
visibleIndex = 0;
}
}
switchVisible();
#text1, #text2, #text3 {
display: none;
}
<div id="text1">text 1</div>
<div id="text2">text 2</div>
<div id="text3">text 3</div>
<input id="Button1" type="button" value="Click" onclick="switchVisible();"/>
A class would be better to mark all elements of a collection.
Sidenote: In my experience classes are almost always better, as they can be applied to a single or to multiple elements.
IDs are imo. more for special cases. They address a single element which must be unique throughout the entire page.
Now back to topic:
About the toggling, you can either keep track of the visible element in a variable:
let visible = 0;
function switchVisible() {
const elements = Array.from(document.querySelectorAll(".tab"));
elements.forEach((element, index) => {
element.style.display = index === visible ? "block" : "none";
});
// increment visible
visible = (visible + 1) % elements.length;
}
.tab {
display: none;
}
<div class="tab">text 1</div>
<div class="tab">text 2</div>
<div class="tab">text 3</div>
<input id="Button1" type="button" value="Click" onclick="switchVisible();" />
or you find out which one is visible in the function itself.
function switchVisible() {
const elements = Array.from(document.querySelectorAll(".tab"));
// checking which one is the first that has some dimension
let visible = elements.findIndex(element => element.offsetWidth || element.offsetHeight);
// increment visible
visible = (visible + 1) % elements.length;
// make this "next one" visible
elements.forEach((element, index) => {
element.style.display = index === visible ? "block" : "none";
});
}
.tab {
display: none;
}
<div class="tab">text 1</div>
<div class="tab">text 2</div>
<div class="tab">text 3</div>
<input id="Button1" type="button" value="Click" onclick="switchVisible()" />
I'm very new to using Javascript and i'm struggling how I can achieve what I am after. I've created 4 buttons using;
<input type="button" name="answer" value="Brave" onclick="showDiv()">
My goal is that if you click on the button, it changes state and the div appears (got that far). If I click another button, i'd like the content to hide the previous div selected and show the one they had just clicked.
Any help/guidance would really be appreciated.
function showDiv() {
document.getElementById('BraveDiv').style.display = "block";
}
function showDiv1() {
document.getElementById('DeterminedDiv').style.display = "block";
}
function showDiv2() {
document.getElementById('CompassionateDiv').style.display = "block";
}
function showDiv3() {
document.getElementById('ConsiderateDiv').style.display = "block";
}
My aim is that if you was to click
function showDiv()
{
document.getElementById('new1').style.display = "block";
document.getElementById('Div1').style.display = "none";
document.getElementById('Div2').style.display = "none";
}
function showDiv1()
{
document.getElementById('Div1').style.display = "block";
document.getElementById('new1').style.display = "none";
document.getElementById('Div2').style.display = "none";
}
function showDiv2()
{
document.getElementById('Div2').style.display = "block";
document.getElementById('new1').style.display = "none";
document.getElementById('Div1').style.display = "none";
}
Your code attached won't achieve any of the results you're looking for, however, it's obvious what you're looking for.
You buttons should look like the following :
<button role="button" onclick="showDiv('BraveDiv')">Brave</button>
Here, the role prevents the default behaviour of submit. The onclick tells the button what to do when you click it, and the "BraveDiv" is the parameter we will pass to the function, telling it which div to display.
The DIV associated with the above button, should look as follows :
<div id="BraveDiv" style="display: none;"> SOME CONTENT HERE </div>
Here you'll notice the ID is equal to the parameter we mentioned above.
And your JavaScript should work as follows :
<script>
function showDiv(elem){
document.getElementById(elem).style.display = "block";
}
</script>
I've attached a working snipped example as below, just click "Run code snippet" to view the snippet and test the code.
function showDiv(elem) {
document.getElementById(elem).style.display = "block";
}
<button role="button" onclick="showDiv('BraveDiv')">Brave</button>
<button role="button" onclick="showDiv('CompassionateDiv')">Compassionate</button>
<div id="BraveDiv" style="display: none;"> SOME BRAVE CONTENT HERE </div>
<div id="CompassionateDiv" style="display: none;"> SOME COMPASSIONATE CONTENT HERE </div>
The above, however, will only SHOW YOUR DIVS.
The full jQuery solution to this (hide/show as per the tag) would be :
<script>
function showDiv(elem) { // When the button is pressed
$("div").each(function() { // For each Div
if ($(this).attr('id') != elem) { // If the Div's id is not equal to the parameter
$(this).css("display", "none");
} // HIDE IT
else {
$(this).css("display", "block"); // SHow It
});
</script>
If you are unfamiliar with jQuery and would prefer a JavaScript only solution, then :
<script>
function showDiv(elem){
var divsToCheck = ["BraveDiv", "CompassionateDiv"]; // Add to here to check more divs
for(let i = 0; i < divsToCheck.length; i++){
if(divsToCheck[i] == elem){
document.getElementById(divsToCheck[i]).style.display = "block";
}
else{
document.getElementById(divsToCheck[i]).style.display = "none";
}
}
</script>
Again I've attached a snippet below.
function showDiv(elem) {
var divsToCheck = ["BraveDiv", "CompassionateDiv"]; // Add to here to check more divs
for (var i = 0; i < divsToCheck.length; i++) {
if (divsToCheck[i] == elem) {
document.getElementById(divsToCheck[i]).style.display = "block";
} else {
document.getElementById(divsToCheck[i]).style.display = "none";
}
}
}
<button role="button" onclick="showDiv('BraveDiv')">Brave</button>
<button role="button" onclick="showDiv('CompassionateDiv')">Compassionate</button>
<div id="BraveDiv" style="display: none;"> SOME BRAVE CONTENT HERE </div>
<div id="CompassionateDiv" style="display: none;"> SOME COMPASSIONATE CONTENT HERE </div>
function showDiv() {
document.getElementById('BraveDiv').style.display = "block";
document.getElementById('DeterminedDiv').style.display = "none";
document.getElementById('CompassionateDiv').style.display = "none";
document.getElementById('ConsiderateDiv').style.display = "none";
}
function showDiv1() {
document.getElementById('BraveDiv').style.display = "none";
document.getElementById('DeterminedDiv').style.display = "block";
document.getElementById('CompassionateDiv').style.display = "none";
document.getElementById('ConsiderateDiv').style.display = "none";
}
function showDiv2() {
document.getElementById('BraveDiv').style.display = "none";
document.getElementById('DeterminedDiv').style.display = "none";
document.getElementById('CompassionateDiv').style.display = "block";
document.getElementById('ConsiderateDiv').style.display = "none";
}
function showDiv3() {
document.getElementById('BraveDiv').style.display = "none";
document.getElementById('DeterminedDiv').style.display = "none";
document.getElementById('CompassionateDiv').style.display = "none";
document.getElementById('ConsiderateDiv').style.display = "block";
}
This might not be the sleekest way of doing it, but will get you the results you want. As each button is pressed, all others will close.
You just need to set the display style of the remaining <div>s back to none. The simplest way to do this is to first set all of them to none, then the one you want visible to block:
Note: I’ve used a function which takes the id of the target <div> as a parameter to reduce the amount of code written, but you could easily copy-paste out to separate functions if you require.
function showDiv(divName) {
// First hide all the divs
document.getElementById('BraveDiv').style.display = 'none';
document.getElementById('DeterminedDiv').style.display = 'none';
document.getElementById('CompassionateDiv').style.display = 'none';
document.getElementById('ConsiderateDiv').style.display = 'none';
// Then show the div corresponding to the button clicked
document.getElementById(divName).style.display = 'block';
}
<input type="button" value="Brave" onclick="showDiv('BraveDiv')">
<input type="button" value="Determined" onclick="showDiv('DeterminedDiv')">
<input type="button" value="Compassionate" onclick="showDiv('CompassionateDiv')">
<input type="button" value="Considerate" onclick="showDiv('ConsiderateDiv')">
<div id="BraveDiv" style="display: none">BraveDiv</div>
<div id="DeterminedDiv" style="display: none">DeterminedDiv</div>
<div id="CompassionateDiv" style="display: none">CompassionateDiv</div>
<div id="ConsiderateDiv" style="display: none">ConsiderateDiv</div>
There are alternative ways of doing this which require less code, such as this method using a little CSS and document.querySelectorAll():
function showDiv(divName) {
// First remove the selected class from all divs in output-divs
document.querySelectorAll('#output-divs > .selected').forEach(element => {
element.classList.remove('selected');
});
// Then add it to the div corresponding to the button clicked
document.getElementById(divName).classList.add('selected');
}
.output-div:not(.selected) {
display: none;
}
<input type="button" value="Brave" onclick="showDiv('brave')">
<input type="button" value="Determined" onclick="showDiv('determined')">
<input type="button" value="Compassionate" onclick="showDiv('compassionate')">
<input type="button" value="Considerate" onclick="showDiv('considerate')">
<div id="output-divs">
<div class="output-div selected" id="brave">Brave</div>
<div class="output-div" id="determined">Determined</div>
<div class="output-div" id="compassionate">Compassionate</div>
<div class="output-div" id="considerate">Considerate</div>
</div>
$(document).ready(function() {
$("#btn1").click(function(){
showDiv('div1');
});
$("#btn2").click(function(){
showDiv('div2');
});
$("#btn3").click(function(){
showDiv('div3');
});
$("#btn4").click(function(){
showDiv('div4');
});
});
function showDiv(_divId){
$(".div-class").each(function() {
if(!$(this).hasClass('div-hide'))
$(this).addClass('div-hide');
});
$('#' + _divId).removeClass('div-hide');
}
.div-class {
min-height: 50px;
border: 1px solid #eee;
margin: 10px;
padding: 10px;
width: 100%;
}
.div-hide {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button id="btn1">Button 1</button>
<button id="btn2">Button 2</button>
<button id="btn3">Button 3</button>
<button id="btn4">Button 4</button>
<div id="div1" class='div-class div-hide'><h3>Div1 Content </h3></div>
<div id="div2" class='div-class div-hide'><h3>Div2 Content </h3></div>
<div id="div3" class='div-class div-hide'><h3>Div3 Content </h3></div>
<div id="div4" class='div-class div-hide'><h3>Div4 Content </h3></div>
hi I'm trying to use an array to change colors. I want to Make a function called ChangeColor(num) with an argument for numbers and Use the function to change the color of the box so when the button is clicked on, it calls on the function and sends the correct number so that "box.style.backgroundColor = arrName[num];" Heres what i got so far.
<!DOCTYPE html>
<html>
<head>
<style>
#box {
width:200px;
height:200px;
background-color:black;
}
</style>
</head>
<body>
<div id="group">
<button id="blue">Blue</button>
<button id="red">Red</button>
<button id="green">Green</button>
</div>
<div id="box"></div>
<script type="text/javascript">
var colors = ["blue","red","green"];
var blue = document.getElementById("blue");
var red = document.getElementById("red");
var green = document.getElementById("green");
var box = document.getElementById("box");
var numclicks = 0;
blue.addEventListener("click", function() {
if(numclicks == 0) {
box.style.backgroundColor = colors[0];
}
});
red.addEventListener("click", function() {
if(numclicks == 0) {
box.style.backgroundColor = colors[1];
}
});
green.addEventListener("click", function() {
if(numclicks == 0) {
box.style.backgroundColor = colors[2];
}
});
</script>
</body>
</html>
You can simply attach an event listener to the buttons within #group and set the background-color of the #box the id of the clicked button:
var box = document.querySelector('#box');
document
.querySelectorAll('#group button')
.forEach(function (el) {
el.addEventListener('click', function () {
box.style.backgroundColor = el.id;
});
});
#box {
width:200px;
height:200px;
background-color:black;
}
<div id="group">
<button id="blue">Blue</button>
<button id="red">Red</button>
<button id="green">Green</button>
</div>
<div id="box"></div>
standard function
const colors = ["blue","red","green"];
const defaultColor = "white"; // if you want for kill errors
function changeColor(num){
document.querySelector("#box").style.backgroundColor = colors[num]||defaultColor
}
then you can added onclick events to buttons like this
<div id="group">
<button onclick="changeColor(0)" id="blue">Blue</button>
<button onclick="changeColor(1)" id="red">Red</button>
<button onclick="changeColor(0)" id="green">Green</button>
</div>
or with attributes like (but keep buttons elements depend to same order of array colors names)
html
<div id="group">
<button number="0" id="blue">Blue</button>
<button number="1" id="red">Red</button>
<button number="2" id="green">Green</button>
</div>
javascript
document.querySelectorAll("#group button").forEach((button)=>{
button.addEventListener('click', function () {
changeColor(button.getAttr("number"));
});
});
The other solutions use practices that are currently considered better. Here is a solution that includes the unnecessary array.
function changeColor(num) {
var colors = ['blue', 'red', 'green'];
document.getElementById('box').style.backgroundColor = colors[num];
}
#box {
width: 200px;
height: 200px;
background-color: black;
}
<div id="group">
<button id="blue" onclick="changeColor(0)">Blue</button>
<button id="red" onclick="changeColor(1)">Red</button>
<button id="green" onclick="changeColor(2)">Green</button>
</div>
<div id="box"></div>
i've got 20/30 divs.
If i click on a button the onClick will tell the function to show welcomeDiv1
but it should also hide welcomeDiv2/3/4/5/6 etc..
Same with showing welcomeDiv7, then it needs to hide welcomeDiv1/2/3/4/5/6/8/9 etc..
Script:
function showDiv1() {
document.getElementById('welcomeDiv').style.display = "none";
document.getElementById('welcomeDiv1').style.display = "block";
}
^^ Now it actually should hide all divs named welcomeDiv.. expect welcomeDiv1
First code
<div class="websites">
<div id="welcomeDiv" style="display:none;" class="answer_list" ><object data="cv/cv.pdf" type="application/pdf" width="100%" height="1100px;" class="cv">
</object></div>
<div id="welcomeDiv1" style="display:none;" class="answer_list" ><object data="cv/6.pdf" type="application/pdf" width="100%" height="1100px;" class="cv">
</object></div>
</div>
And second code
<a title='Project 1'class="text1" onclick="showDiv()">Project 1</a>
<script>
function showDiv() {
document.getElementById('welcomeDiv1').style.display = "none";
document.getElementById('welcomeDiv').style.display = "block";
}
</script>
<a title='Project 2' class="text1" onclick="showDiv1()">Project 2</a>
<script>
function showDiv1() {
document.getElementById('welcomeDiv').style.display = "none";
document.getElementById('welcomeDiv1').style.display = "block";
}
</script>
You must use for loops, for example
function showDiv(div){
for (i = 0; i => 100; i++){
var x = document.getElementById('exampleDiv-' + i);
x.style.display = 'none';
//You can also use visibility to get animations work.
}
var y = document.getElementById('exampleDiv-' + i);
y.style.display = 'block';
}
You can refactor your HTML/Script. Persist target with the element, which can be later retrieved using Element.dataset property.
Learn to use addEventListener() to attach event handler.
Here is a sample snippet:
document.querySelectorAll('.button').forEach(function(element) {
element.addEventListener('click', function() {
var _this = this;
document.querySelectorAll('.welcome').forEach(function(welcome) {
if (_this.dataset.target == welcome.id) {
welcome.style.display = "block";
} else {
welcome.style.display = "none";
}
})
});
});
.welcome {
display: none
}
<button type="button" class="button" data-target="welcome1">1</button>
<button type="button" class="button" data-target="welcome2">2</button>
<button type="button" class="button" data-target="welcome3">3</button>
<br/>
<div class="welcome" id="welcome1">welcome1</div>
<div class="welcome" id="welcome2">welcome2</div>
<div class="welcome" id="welcome3">welcome3</div>
Also give each div a class WelcomeDiv. Then, you just hide the entire WelcomeDiv class and show the one you want. For example:
divs = document.getElementsByClassName("WelcomeDiv");
for (i = 1; i < divs.length; i++) {
divs[i].style.display = "none";
}
document.getElementById("WelcomeDiv1").style.display = "block";
A simple and easy solution for you:
<a title='Project 1'class="text1" onclick="showDiv('welcomeDiv1')">Project 1</a>
<a title='Project 2' class="text1" onclick="showDiv('welcomeDiv2')">Project 2</a>
<div class="websites">
<div id="welcomeDiv1" style="display:none;" class="answer_list" >1</div>
<div id="welcomeDiv2" style="display:none;" class="answer_list" >2</div>
</div>
<script>
function showDiv(div_id) {
var divsToHide = document.getElementsByClassName("answer_list"); //divsToHide is an array
for(var i = 0; i < divsToHide.length; i++){
divsToHide[i].style.display = "none"; // hide your divisions
}
document.getElementById(div_id).style.display = "block"; //show your division
}
</script>
Alright i fixed it my self, with some help of the lovely internet
function MyFunction(divName){
//hidden val
var hiddenVal = document.getElementById("tempDivName");
//hide old
if(hiddenVal.Value != undefined){
var oldDiv = document.getElementById(hiddenVal.Value);
oldDiv.style.display = 'none';
}
//show div
var tempDiv = document.getElementById(divName);
tempDiv.style.display = 'block';
//save div ID
hiddenVal.Value = document.getElementById(divName).getAttribute("id");
}
And HTML 1:
<input id="tempDivName" type="hidden" />
<a title='Project 1'class="text1" OnClick="MyFunction('myDiv1');">Project 1</a>
<a title='Project 2' class="text1" OnClick="MyFunction('myDiv2');">Project 2</a>
<a title='Project 3' class="text1" OnClick="MyFunction('myDiv3');">Project 3</a>
And html 2:
<div id="myDiv1" style="display:none;" class="answer_list" ><object data="cv/cv.pdf" type="application/pdf" width="100%" height="1100px;" class="cv">
</object></div>
<div id="myDiv2" style="display:none;" class="answer_list" ><object data="cv/6.pdf" type="application/pdf" width="100%" height="1100px;" class="cv">
</object></div>
<div id="myDiv3" style="display:none;" class="answer_list" ><object data="cv/Practiced.pdf" type="application/pdf" width="100%" height="1100px;" class="cv">
</object></div>
If you are trying without a loop as you have n set of a and div(loop through more elements affects performance), then I would suggest to go with adding show class to the showing div
function showDiv(t) {
if (document.querySelector('.show'))
document.querySelector('.show').className = "welcome";
document.getElementById(t.dataset.target).className = "welcome show";
}
.welcome {
display: none;
}
.show {
display: block;
}
.button {
background-color: #0095ff;
border-color: #07c;
cursor:pointer;
padding:0px 20px;
}
<button type="button" class="button" data-target="welcome1" onclick="showDiv(this)">1</button>
<button type="button" class="button" data-target="welcome2" onclick="showDiv(this)">2</button>
<button type="button" class="button" data-target="welcome3" onclick="showDiv(this)">3</button>
<br/>
<div class="welcome" id="welcome1">welcome1</div>
<div class="welcome" id="welcome2">welcome2</div>
<div class="welcome" id="welcome3">welcome3</div>
Using radio button
If you are trying without a loop as you have n set of a and div, then I would suggest to go with a hidden radio button method
function showDiv(t) {
document.getElementById(t.rel).click();
}
.answer_list {
display: none;
}
.webrad {
display: none;
}
.webrad:checked+.answer_list {
display: block;
}
.text1 {
background-color: #0095ff;
border-color: #07c;
cursor:pointer;
padding:0px 20px;
}
<div class="websites">
<a title='Project 1' rel="welcomeRadio" class="text1" onclick="showDiv(this)">Project 1</a>
<a title='Project 2' rel="welcomeRadio1" class="text1" onclick="showDiv(this)">Project 2</a>
</div>
<div class="websites">
<input id="welcomeRadio" class="webrad" type="radio" name="websites" />
<div id="welcomeDiv" class="answer_list">cv.pdf<object data="cv/cv.pdf" type="application/pdf" width="100%" height="100px;" class="cv">
</object></div>
<input id="welcomeRadio1" class="webrad" type="radio" name="websites" />
<div id="welcomeDiv1" class="answer_list">6.pdf<object data="cv/6.pdf" type="application/pdf" width="100%" height="100px;" class="cv">
</object></div>
</div>
I have two div in my html file. I want to hide the 1st div and show another div on html input button onclick event.
Here is my code,
function switchVisible() {
if (document.getElementById('Div1') !== undefined) {
if (document.getElementById('Div1').style.display == 'Block') {
document.getElementById('Div1').style.display = 'none';
document.getElementById('Div2').style.display = 'Block';
} else {
document.getElementById('Div1').style.display = 'Block';
document.getElementById('Div2').style.display = 'none';
}
}
}
#Div2 {
display: none;
}
<input id="Button1" type="button" value="" onclick="javascript:switchVisible();" />
But it's not working. Any help will be appreciated.
Thanks.
1) Inside onclick, you don't have to use "javascript:", that is implied.
2) You check for "display: block", I always check for "display: none" (Because the display can also be "inline-block", etc.)
Try this:
function switchVisible() {
if (document.getElementById('Div1')) {
if (document.getElementById('Div1').style.display == 'none') {
document.getElementById('Div1').style.display = 'block';
document.getElementById('Div2').style.display = 'none';
}
else {
document.getElementById('Div1').style.display = 'none';
document.getElementById('Div2').style.display = 'block';
}
}
}
#Div2 {
display: none;
}
<div id="Div1">Div 1</div>
<div id="Div2">Div 2</div>
<input id="Button1" type="button" value="Click" onclick="switchVisible();"/>
As it is tagged with jQuery, here's the simple jQuery answer
$('body').on("click touchstart", "#Button1", function(e){
$("#Div1, #Div2").toggle();
});
use on to listen for the id #Button, I've used both click and touchstart to make it mobile friendly, and then used toggle() which sets the state of the display: to the opposite to what it is now. So if it was display:none, it becomes display:block, if it was display:block it becomes display:none
Try this:
var div1 = document.getElementById('Div1'),
div2 = document.getElementById('Div2');
function switchVisible() {
if(!div1) return;
if (getComputedStyle(div1).display == 'block') {
div1.style.display = 'none';
div2.style.display = 'block';
} else {
div1.style.display = 'block';
div2.style.display = 'none';
}
}
document.getElementById('Button1').addEventListener('click', switchVisible);
#Div2 {
display:none;
}
<div id="Div1">Div 1</div>
<div id="Div2">Div 2</div>
<input id="Button1" type="button" value="Click me" />
However, this approach may be better:
var wrapper = document.getElementById('wrapper');
function switchVisible() {
wrapper.classList.toggle('switched');
}
document.getElementById('Button1').addEventListener('click', switchVisible);
#wrapper > :last-child {
display: none;
}
#wrapper.switched > :last-child {
display: block;
}
#wrapper.switched > :first-child {
display: none;
}
<div id="wrapper">
<div>Div 1</div>
<div>Div 2</div>
</div>
<input id="Button1" type="button" value="Click me" />
you might wanna try this..
function switchVisible() {
var div1=document.getElementById('Div1');
var div2=document.getElementById('Div2');
if (div1 !== undefined && div2 !== undefined) {
div1.style.display = div2.style.display === '' ? 'none' : div2.style.display === 'none' ? 'none' : 'block';
div2.style.display = div1.style.display === 'block' ? 'none' : 'block';
}
}
#Div1{
display: block;
background: blue;
}
#Div2 {
display: none;
background: red;
}
.divs
{
width: 50px;
height: 50px;
border: 1px solid #000;
}
<input id="Button1" type="button" value="Hide" onclick="switchVisible();" />
<div id="Div1" class="divs"> </div>
<div id="Div2" class="divs"> </div>
Probably you have some syntax errors in your code. This one is working:
function switchVisible() {
if (document.getElementById('Div1') !== undefined) {
if (document.getElementById('Div1').style.display == 'block') {
document.getElementById('Div1').style.display = 'none';
document.getElementById('Div2').style.display = 'block';
} else {
document.getElementById('Div1').style.display = 'block';
document.getElementById('Div2').style.display = 'none';
}
}
}
#Div1 {
display:none;
}
#Div1 {
background: red;
}
#Div2 {
background: green;
}
#Div1, #Div2 {
width: 50px;
height:50px;
}
<div id="Div1"></div>
<div id="Div2"></div>
<input id="Button1" type="button" value="" onclick="javascript:switchVisible();" />
Don't add the click event through html. Add an event listener like document.getElementById ("Button1").addEventListener ("click", switchVisible, false);
See working jsFiddle here: http://jsfiddle.net/bevanr01/gmkconLw/