Iv'e created a dropdown that requires you to click on text to show more text underneath. The code is:
.dropbtn {
cursor: pointer;
font-weight: bold;
}
.dropdown03 {
position: relative;
display: inline-block;
text-align: left;
width: 100%;
}
.dropdown-content03 {
display: none;
position: relative;
width: 100%;
overflow: auto;
z-index: 1;
}
.show {display: block;}
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
if (!event.target.matches('.dropbtn')) {
var dropdowns = document.getElementsByClassName("dropdown-content03");
var i;
for (i = 0; i < dropdowns.length; i++) {
var openDropdown = dropdowns[i];
if (openDropdown.classList.contains('show')) {
openDropdown.classList.remove('show');
}
}
}
}
</script>
<div class="dropdown03">
<button onclick="myFunction()" class="dropbtn">Dropdown Button Text</button>
<div id="myDropdown" class="dropdown-content03">
Dropdown Button Text
</div>
</div>
This code has works perfectly in my website and when you click on "Dropdown Button Text" then "Dropdown Button Text" appeasers below it. Here is my trouble; when i add another dropdown using the same code, the first dropdown on the website will show but the one that the button is above doesn't. In other words when i add more then one dropdown all the new ones don't work they only end up showing the first dropdowns content. (hopefully this makes sense)
How can i have more than one dropdown that will load is own content when clicked? Do i have to create a whole new tag for each one?
Changing the names of id's and classes would help. just try by doing so. Also check for the console by clicking the inspect button. Take a look at your HTML from inspect tab. Make sure there are no errors in the console.
Make sure while reloading the browser, your hard refresh by Ctrl+F5.
Related
I want to make a button that changes the text inside the button by pressing the button, but I don't know how! :(
I used :hover, but when I move the mouse pointer away, it goes back to its previous state.
There is the possibility of solving it with the pseudo-class :hover and the use of data attributes. The idea of this solution is that you hide the original button text, add an empty content and then use hover over the element to show the content of the data attribute.
I'll show you how in the following example:
body {
background-color: #F2CD5C;
text-align: center;
}
.container {
margin-top: 30vh;
}
/*
MARK BUTTON:
In the button styles, it is necessary to hide the original text that we generated, to create the correct spacing and the data attribute text can overlap
The text color must be the same as the button background.
Position must be relative.
*/
.button {
position: relative;
padding: 0.5em 1em;
border: 2px solid #fff;
border-radius: 15px;
font-size: 2em;
color: black;
background: black;
}
/*
MARK USE :before and :after
Setup pseudo-element ::before with content: ""; and position must be absolute and setup with the original position text inside the button.
Write ::after with the exact text inside button = Click me! with the same position and setup to ::before pseudo-element
*/
.button::before {
content: "";
position: absolute;
left:0;
width: 100%;
text-align: center;
opacity: 0;
color: white;
}
.button::after {
content: "Click me!";
position: absolute;
left:0;
width: 100%;
text-align: center;
opacity: 1;
color: white;
}
.button::before {
content: attr(data-hover);
}
.button:hover:before{
opacity: 1;
}
.button:hover:after{
opacity: 0;
}
<div class="container">
<button class="button" type="button" data-hover="Hello world!">Click me!</button>
</div>
The code uses pseudo-elements ::before and ::after to display different text when the button is hovered over. The text "Click me!" is set as the content for the ::after pseudo-element, while the ::before pseudo-element gets its content from the "data-hover" attribute. When the button is hovered over, the ::before pseudo-element's opacity becomes 1 and the ::after pseudo-element's opacity becomes 0, effectively hiding and showing different text on the button.
I hope this can help you solve your question. Anyway, this solution is not clean, we should handle the DOM using JavaScript.
Reference Links
Using data attributes
I only know how to do this using javascript, hope that helps.
HTML
<button class="btn">Hey, click me!</button>
JS
first I store the button in a variable
var button = document.querySelector(".btn")
then I add an event listener to the button with the "click" event, which will make the function "function()" be executed whenever the button is clicked
button.addEventListener("click", function(){})
now, I define the function to change the text of the button using "this" to access the button of the function and ".textContent" to change the text that was inside
button.addEventListener("click", function(){
this.textContent = "Hey, you clicked me!"
})
Click "run" for a preview
var button = document.querySelector(".btn")
button.addEventListener("click", function(){
this.textContent = "Hey, you clicked me!"
})
<button class="btn">Hey, click me!</button>
I have a button that toggles active class on a div.
When the div has active class it is displayed and when active class is removed the div is hidden.
I want active class removed from the div when the page is loaded, but later when I click the button active class should be applied. The div should be hidden when the page loads.
I don't have access to HTML so it has to be done using jquery.
here is my code:
<button class="btn addclass">Toggle class</button>
<div class="block active">
</div>
my js:
$(document).ready(function(){
$(window).on('load', function(){
if($(".addclass .block").hasClass("active")){
$(this).removeClass("active");
}
});
});
$(document).ready(function(){
$(".addclass").click(function(){
$(".block").toggleClass("active");
});
});
CSS
.block{
display: none;
padding: 50px;
background-color: red;
}
.block.active{
display: block;
}
In this block of code:
$(document).ready(function(){
$(window).on('load', function(){
if ($(".addclass .block").hasClass("active")) {
$(this).removeClass("active");
}
});
this refers to the window so you can't use window here.
You also don't shouldn't nest window.load inside doc.ready (use one or the other). While doc.ready will fire even if the document is already ready, window load will only fire at the one time that it loads and that will be before doc.ready runs, so your code (probably) never runs.
Your code can be shortened to:
$(function() {
$(".addclass .block.active").removeClass("active");
});
Since jQuery specializes in controlling all things DOM it's a shade faster and less flashing if you listen for the "DOMContentLoaded" event on the document object. By that time all of the DOM is loaded and the scripts have been parsed. If you listen to the "load" event on the window object, then everything has been loaded, that includes all the stuff you didn't need to run jQuery.
BTW if you don't already know, it's important that all <script> elements be placed right before the closing </body> tag. Of course this may not be possible for you since you have to deal with 3rd party code at page load. See this article about what was just discussed.
/*
Once document is loaded
Remove .active from .block
*/
$(document).on('DOMContentLoaded', e => $('.block').removeClass('active'));
/* ALTERNATIVE
Once document is loaded
Hide .block
/
$(document).on('DOMContentLoaded', e => $('.block').hide());
*/
/*
When .toggle is clicked...
Toggle .block to hide/show
Toggle the text of .toggle to "SHOW/HIDE"
*/
$('.toggle').on('click', function(e) {
$(this).text($(this).text() === 'SHOW' ? 'HIDE' : 'SHOW');
/*
Toggle .active class on/off on .block
*/
$('.block').toggleClass('active');
/* ALTERNATIVE
Toggle .block show/hide slowly
/
$('.block').toggle('slow');
*/
});
body {
font: 5ch/1 Consolas;
text-align: center;
}
button {
font: inherit;
cursor: pointer;
}
.block {
display: none;
}
.active {
display: block;
}
<button class="btn toggle">SHOW</button>
<div class="block active">ACTIVE</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
I want to make this:
<div class-name="search">
<input class-name="input"/>
<button class-name="searchbutton> search </button>
</div>
but I want the button to be hidden, but when I hover the .search div or the .input has value in it, I want the button to appear, how can I do that?
You could start from :
.input + button {
display:none;
}
.input:hover + button ,.input:valid + button ,.input:focus + button {
display:initial;
}
<div class-name="search">
<input class="input" required />
<button class="searchbutton"> search </button>
</div>
Update from comment :
Hi , I do not wish to make the input invisible, just the button, and I want the search div to be the element when it was hovered to be showing the button, can I do that? – Alex K
.input + button {
display:none;
}
.search:hover button {
display:initial;
}
<div class="search">
<input class="input" required />
<button class="searchbutton"> search </button>
</div>
You can do this with "display", first try making the searchbutton invisible by doing so...
<button class-name="searchbutton style="display: none;"> search </button>
Then you can check if the user is hovering over the search to make the button appear
.search:hover {
.searchbutton {
display: inline-block;
}
}
Also check if the input is selected
.input {
.searchbutton {
display: inline-block;
}
}
You can use a simple JavaScript event to easily hide and show the button.
var search = document.querySelector("[class-name=search]");
var searchButton = document.querySelector("[class-name=searchbutton]");
var input = document.querySelector("[class-name=input]");
function showSearchButton() {
searchButton.style.display = "block";
}
function hideSearchButton() {
searchButton.style.display = "none";
}
search.onmouseover = showSearchButton;
input.onchange = showSearchButton;
//If you need to hide the element once they stop hovering
search.onmouseleave = hideSearchButton;
And for your CSS simply have the element hidden in the first place until it is hovered over or the input value changes.
[class-name=searchbutton] {
display: none;
}
And I do recommend switching the "class-name" to simply "class" to make it easier to work with.
I'm looking for a solution for a bootstrap accordion with a button to expand the accordion, but I would like to hide this button when it is expanded.
My accordion headline is like the beginning of the text inside of the accordion, and if I have the button there it is disturbing the view.
How can I hide this button when the accordion is expanded, but visible again when the accordion is collapsed?
The problem here is, that when I click on the title, to close the accordion, then the button remains disappeared, and if I click on the title again, the accordion will expand, but the title will disappear as well. If Possible, I would like to keep the title as link.
a[data-toggle='collapse'].collapsed {
visibility: visible;
}
a[data-toggle='collapse'] {
visibility: hidden;
}
This is what I have so far:
https://jsfiddle.net/o93kwj80/2/
Thanks
How will you close the accordion if you hide the title? I made a solution for you. If you click on the title, the title will be hidden and a close icon will show up. Then click on the close icon and the title will show up and accordion will be closed and close icon will be hidden.
a[data-toggle='collapse'].collapsed .title {
display: block;
}
a[data-toggle='collapse'].collapsed .close {
display: none;
}
a[data-toggle='collapse'] .title {
display: none;
}
a[data-toggle='collapse'] .close {
display: initial;
}
.close {
float: right;
color: red;
}
Please check the updated fiddle:
https://jsfiddle.net/o93kwj80/3/
I have a button in my webpage with below code -
HTML:
<button type="submit" class="checkout-button" id="checkout-button" name="checkout-button"></button>
CSS:
.checkout-button{
width: 130px;
height: 35px;
background: url('../poc2/images/checkout.png') no-repeat;
border: none;
vertical-align: top;
margin-left:35px;
cursor:pointer;
}
Now, the button works fine as I can click on it and have my corresponding php code run; the pointer turns into the hand symbol letting the user clearly know that it is clickable and that its a button.
What I would like to do is to modify the behavior of this button based on some conditions. Example pseudocode:
if flag=1:
/* enable the button, and let the user click it and run the php code */
else:
/* display this button, but don't let any actions take place if the user clicks on it; */
How do I code this enable-disable logic for the button? Basically, I want the button to work as a button under normal situations; and just as a picture without any hyperlink under certain other situations.
You can either do this without JavaScript (requires a page refresh) or with JavaScript and have no refresh.
Simply use the disabled attribute:
<button type="submit" class="checkout-button" id="checkout-button" name="checkout-button" disabled="disabled"></button>
And create a css style for it, if necessary. The example below shows a JavaScript solution. If the variable disableButton is set to true, the button will be disabled, else it can be clicked:
const disableButton = true; //change this value to false and the button will be clickable
const button = document.getElementById('checkout-button');
if (disableButton) button.disabled = "disabled";
.checkout-button {
width: 130px;
height: 35px;
background: #333;
border: none;
vertical-align: top;
margin-left: 35px;
cursor: pointer;
color: #fff;
}
.checkout-button:disabled {
background: #999;
color: #555;
cursor: not-allowed;
}
<button type="submit" class="checkout-button" id="checkout-button" name="checkout-button">submit</button>
You can do it either by modifying the attribute or by adding/removing a class.
Modifying attribute
You will want to switch between <button disabled="true"> and <button disabled="false">
With javascript, it could look like this:
if flag=1:
document.getElementById("your-btn").disabled = true;
else:
document.getElementById("your-btn").disabled = false;
And with jquery, like this:
if flag=1:
$('#your-btn').prop('disabled', true);
else:
$('#your-btn').prop('disabled', false);
Adding/removing class
Add the following css:
.btn-disabled{
cursor: not-allowed;
pointer-events: none;
}
And add/remove a class to the button.
With jquery:
if flag=1:
$('#your-btn').addClass('btn-disabled');
else:
$('#your-btn').removeClass('btn-disabled');
If you don't want jquery, but pure javascript, here is how to do it.
If your circumstance allows you could just remove the content in the action attribute from the form tag. Therefor when a user clicks submit, no action is taken.