Show DIV only after Click on 3 Buttons - html

I want to make a DIV which appear only when someone click on 3 buttons.
I searched through the posts and find something like, Hide/Show DIV onClick Button but i can't find anything like show DIV only after Click on all 3 buttons.I tried to modify a few but had no luck.
Anyone point me in the direction for example? Would like to do this in JS or jQuery.
Any help would be much appreciated.Thanks in Advance

I would do it by making an if-statement that passes if a variable counter you create reaches 3. Every click to one of 3 buttons adds to the counter once, and at 3 it passes the if-statement and the div appears.
For example, you should understand this code:
var counter = 0;
if(counter == 3) {
console.log("Show div!");
}
So your job is to figure out how to do this with your current situation, and instead of just console.log something random, to show the div you wish to show when the counter reaches 3.
It's the same if your buttons are 3 separate ones, just add to the counter upon each click.
So in your callback function inside JQuery's .click() function you can add to the counter every time someone clicks one of three buttons, and some where else in your js file you can make an if-statement checking if that variable has reached "3". If it has, make the div appear.
I suggest you work on this problem in this way before making improvements, such as considering how to improve this code so that it only works upon clicking each of the three unique buttons once. Breaking down problems is very important in programming.
One tip I have for when you reach this point is say you have 3 buttons with unique ids: 'btn-1', 'btn-2', 'btn-3', think of a way of making the if statement only fire off if each of those unique buttons are clicked.
See if you can get this to work yourself, and feel free check back with some sample code for more help.

You need to have one variable that acts as counter. And you need to make every of those buttons clickable only once.
<button class="clickableOnce">Btn 1</button>
<button class="clickableOnce">Btn 1</button>
<button class="clickableOnce">Btn 1</button>
<div class="theDiv" id="theDiv">
Javscript code:
var countMyClicks = 0;
jQuery('.clickableOnce').on('click', function() {
countMyClicks++;
jQuery(this).prop('disabled', true);
if(countMyClicks==3){
jQuery('#theDiv').show();
}
});
CSS code:
.theDiv{
background:red; height:10px; width:10px; display:none;
}
And you can check the fiddle here:
https://jsfiddle.net/c2uc44f1/

Make 3 buttons, put the same class in the 3 of them, like this:
<button class="ex">Button1</button>
<button class="ex">Button2</button>
<button class="ex">Button3</button>
Then use JS:
var a = 0;
$(".ex").click(function({
a++;
});

Related

Trying to expand my show/hide DIV code to target multiple divs efficiently

so I have a long case study, so many images it takes forever to load. So one of the steps I'm taking is hiding all images in a section that are more than 3. I'm doing this in a simple way with show and hide jQuery, and manually adding the target ID to the div I want to show. My question I'm trying to figure out is how I can use this code to target every section, instead of one, since I can only use the target ID once. I don't want to simple copy and paste the code and change the target DIV name, as that is going to be very redundant and certainly the most amateur approach. My pen is located here:
<button class="Show">Show</button>
<button class="Hide">Hide</button>
<button class="toggle">Show & Hide</button>
<div id="target"></div>
body {padding:30px;}
#target { display:none;}
.Hide{ display:none;}
$('.Show').click(function() {
$('#target').show(200);
$('.Show').hide(0);
$('.Hide').show(0);
});
$('.Hide').click(function() {
$('#target').hide(500);
$('.Show').show(0);
$('.Hide').hide(0);
});
$('.toggle').click(function() {
$('#target').toggle('slow');
});
Thank you in advance!
I don't understand what you mean and what you want to do, but you can use it this way instead of a few different functions.
As far as I understand you want to use css classes as well?
.hide{display:none}
$("button").click(function() {
$("button").removeClass("hide");
$(this).addClass("hide")
})
I tried to be helpful in a very simple way for you, I hope it will be useful.

How to disable a button after onclick event?

I want to disable this button after the onclick function, so either change the z-index, or disable the button, any ideas?
<button id ="a" type="button"
onclick="window.open('https://www.google.ca')"
>
Thanks.
You need to add this.disabled=true after opening the window.
<button id ="a" type="button"
onclick="window.open('https://www.google.ca'); this.disabled=true;"
>
Code above needs 'disabled' instead of 'disable.' Try this:
<button id ="a" type="button"
onclick="window.open('https://www.google.ca'); this.disabled=true;"
>
The approach is that you should create a script contain function that does two jobs:
make that button disable using selectById() and adding attribute disabled
then window.open()
i would take a look at jquery
https://api.jquery.com/click/
$( "#a" ).click(function() {
// do your stuff opening a page etc
$( "#a" ).prop("disabled",true);
});
edit:// take Rohit Saxena's approach
this is my first time posting, so forgive a noob if I don't get the format correct. I needed to be able to easily turn a button on and off to 'guide' the user to perform actions in the correct order, and this post helped me in that journey, although I only used part of the answer. I made two functions 'enableClick()' and 'disableClick()', where the parameter is the id of the button, eg: 'enableClick("betButton")' Here is the code:
function disableClick (elementId) {
const x = document.getElementById(elementId);
x.disabled = true;
}
I'm learning js, so everything I'm doing is vanilla at this point on purpose, but it's still fairly simple - obviously, with the enableClick function, the value of x.disabled would be 'false'. These functions can be added inside a function called by a click, after the initial click functionality is complete, so that the button can't be clicked again until the opposite function is called.. love this stuff!

Multiple links with delay

I was wondering how to make a link, that when clicked it would open 2
links: One would open in a new tab instantly Second one would open in
a new window after a minute or so.
I also want to embed this in a website like weebly.
This is an edit to make things simpler -
linky
two opens one click
This sounds very spammy and not recommended but if you were to do it, I would do it a separate function rather than in line in the onclick html attribute.
Try the below. It will give a 5 second time gap between links.
function openPage1() {
window.open(url1);
window.setTimeout(openPage2,5000);
}
function openPage2() {
window.open(url2);
}
// Just call openPage1 from your button click handler
openPage1();

Switchable table by pressing button

How can I make a switchable table with buttons to lead to different view of another table? Something like this:
As you can see, once you click on the buttons it takes you to another table list.
Here is a base: http://jsbin.com/agavid/136/edit
Need something similar to this.
You can create several different tables and use a simple JS/jQuery script to show and hide the tables based on which button is pressed.
Essentially you would show all tables at start (for progressive enhancement), then hide all of them except the first one. Then when a button is clicked, hide all the tables and show only the one associated with that button.
Here's a demo of what I'm talking about. http://jsfiddle.net/7Ywbn/2/
(function () {
var tables = $("table");
//Grabs all the tables
tables.hide().first().show();
//Hides all the tables except first
$("a.button").on("click", function () {
//Adds eventListner to buttons
tables.hide();
//Hides all the tables
var tableTarget = $(this).data("table");
//Gets data# of button
$("table#" + tableTarget).show();
//Shows the table with an id equal to data attr of the button
})
})();
Hope I understood your question correctly.
here is a suggestion
wrap each table in a <div id="table1"> <div id="table2"> etc.. and hide them all by default. here is some help on that: hiding div using js
and then only show the <div> for the table associated with the button the user has clicked on. you can do this using javascript, jquery as a couple examples.
you may need to start looking into learning some basic javascript/jquery if you don't already know any, you are going to need it.
good luck.
You could create multiple divisions within the same file and hide all the divisions except the primary you want to display during the initial page load. then use "onClick='showNextDiv();" inside the buttons themselves.
A sample of the javascript would be:
<script type='text/javascript' src='http://code.jquery.com/jquery-1.7.1.js'></script>
<script language="JavaScript" type="text/javascript">
jQuery.noConflict()
function showNextDiv() {
document.getElementById('DivName').style.display = "block";
document.getElementById('FirstDiv').style.display = "none";
}
</script>
All this is saying that when someone clicks on a button this script runs and displays the 'DivName' div. That's what "block" means. It also turns off the division "FirstDiv" by using the "none".
You can add as many "block" and/or "none" statements to the script. If you have 6 divisions you would want to display one and turn off 5.
I hope this is helpful. I'm not an expert at this but it works fine for me.

How to correctly call jQuery function that uses HTML elements?

I'm new to the use of jQuery so the problem I'm facing should be fairly straight forward. Basically what I'm trying to accomplish is load a variety of simple text-only pages within DIV elements of my site, and with a navigation bar hide/unhide these individual DIVs.
DIVs are correctly loaded the requested pages using an script block. However, what is not working correctly is toggling the visibility of these DIV blocks. I've narrowed it down to a jQuery function I've created which blocks the entire script call whenever I refer to any of the DIV blocks. Let me explain better with a code snippet.
This is is some very simple code that, on the click of a menu link, runs a hide function then shows the corresponding DIV element.
$( document ).ready(function()
{
console.log("document ready."); <-- does NOT get called with hideDivs()
$('#button1').click(function(){
hideDivs();
$("#page1").show();
});
$('#button2').click(function(){
hideDivs();
$("#page2").show();
});
});
This is the hideDivs() function, JUST above the ready function:
function hideDivs()
{
$("#page1").hide(); <-- These lines cause the entire
$("#page2").hide(); <-- <script> block to note get called.
}
Finally, page1 and page2 are created with a script block halfway inside the page:
<div id="page1"></div>
<div id="page2"></div>
<script>
$("#page1").html('<object style="overflow:hidden; width: 100%; height: 500px;" data="page1.php">').show();
$("#page2").html('<object style="overflow:hidden; width: 100%; height: 500px;" data="page2.php">').hide();
</script>
Why then is it that the top SCRIPT block fails with the hideDivs() function? I've tried placing it inside the $( document ).ready function with no change. Again, if the function is blank, or contains something simple like 'console.log' it works, but when referring to DIV tags it breaks.
Even stranger, the code that makes the function FAIL, WORKS if I simply rewrite the code as such:
$('#button1').click(function(){
$("#page1").hide(); <-- This works fine
$("#page2").hide(); <-- (page1 repeated to match function code)
$("#page1").show();
});
I have quite a few pages so I would much rather be able to use a function as not to have lots of repetitive code.
I have no errors displayed in my javascript console. I've looked closely at functions calls with StackOverflow and Google searches but couldn't spot a solution. I'm sure I've made a really silly mistake I'm overlooking, so any help would be much appreciated.
So instead of the whole function to hide your divs, you can simply put a class on each one and hide them by selecting that class. For example, each page Div give a class="clickablePages", and then do:
$(".clickablePages").hide();
that will simply hide all the divs that you have added the class to.
As for repeating all the button clicks for each button, you can simply do it in one function based on the id of the button. You can again put a class on all of the buttons as well, trigger the function by selecting the class and then grab the id you need within that function. something like this:
$('.buttonclick').click(function(){
var pageID = $(this).attr('id');
$("#page" + pageID).show();
});
In this case, if your buttons just had an id of '1' or '2' that matched the page number, it would only show the div for that page number. Hope that makes sense.