my html page contains this code:
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var number = getUrlVars()["login"];
if (number == 'success') {
$('.success_box').css("visibility" , "visible");
setTimeout('$(".success_box").css("visibility" , "hidden")', 5000);
} else {
$('.success_box').css("visibility" , "hidden");
}
.success_box {
width: 275px;
background-color: #333;
height: 50px;
border-radius: 10px;
position: absolute;
right: 10px;
top: 60px;
vertical-align: center;
display: flex;
align-items: center;
justify-content: center;
}
.success_box_text {
display: inline-block;
color: rgb(228, 23, 23);
text-decoration: none;
}
.success_box_text_color {
color: #33cc33;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div class="success_box">
<div class="success_box_text">
Login <span class="success_box_text_color">completed ✔</span>
</div>
</div>
Javascript is triggered by url parameter: http://localhost/scarface-master/scarface/index.html?login=success
problem: at page load the success_box is visible for 2 sec although the parameter is not set. After two seconds the success_box switches to hidden. Desired behavior: Box is not visible without parameter.
Any tips to get rid of this?
Add this extra rule for class success_box to your CSS
.success_box {
display:none
}
and replace your JS code with this
if (number == 'success') {
$('.success_box').show();
setTimeout('$(".success_box").hide()', 5000);
}
The problem is it takes JS file some time to load. So you must set the default display state of the box to none
hide as default this element in CSS. Your jquery code runs with delay.
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var number = 'success';
if (number == 'success') {
$('.success_box').css("visibility" , "visible");
setTimeout('$(".success_box").css("visibility" , "hidden")', 5000);
} else {
$('.success_box').css("visibility" , "hidden");
}
.success_box {
visibility: hidden;
width: 275px;
background-color: #333;
height: 50px;
border-radius: 10px;
position: absolute;
right: 10px;
top: 60px;
vertical-align: center;
display: flex;
align-items: center;
justify-content: center;
}
.success_box_text {
display: inline-block;
color: rgb(228, 23, 23);
text-decoration: none;
}
.success_box_text_color {
color: #33cc33;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.2/jquery.min.js"></script>
<div class="success_box">
<div class="success_box_text">
Login <span class="success_box_text_color">completed ✔</span>
</div>
</div>
`
just add
.success_box {
visibility: hidden;
...
in css
Related
So I want to replace the text inside of an input field with a h1 tag as soon as the user hits submit because i want the text to have an animation but i can't animate the text inside the text field.
I linked the code pen project version of it to make it easier then organizing all the code in here. I added all the code I had so I wouldn't leave anything out although some of it may be irrelevant.
Basically I want the h1 tag to appear exactly where the input text was so it looks like nothing ever got replaced.
https://codepen.io/timvancowabunga/pen/rNOqdYd
$(document).ready(function() {
$('#btn1').click(function() {
$('#test').text($("#message").val());
$('#message').val('');
$('#test').val('');
});
});
function onTextClick() {
document.getElementById('btn1').className = "show";
}
function showButton() {
document.getElementById('btn1').style.display = 'block';
}
function showSendButton() {
document.getElementById('btn2').style.display = 'block';
}
function formCheck() {
var input = $('#message').val();
if (input == '') {
alert("Please Submit a Valid Message");
} else {
hideButton();
showSendButton();
}
}
function hideButton() {
document.getElementById('btn1').style.display = 'none';
}
function hideSendButton() {
document.getElementById('btn2').style.display = 'none';
document.getElementById('sent').style.display = 'block';
}
function myMove() {
var textWrapper = document.querySelector('.ml13');
textWrapper.innerHTML = textWrapper.textContent.replace(/\S/g, "<span class='letter'>$&</span>");
anime.timeline()
.add({
targets: '.ml13 .letter',
translateY: [0, -1600],
opacity: [1, 0],
easing: "easeInSine",
duration: 3600,
delay: (el, i) => 800 + 60 * i
});
}
body {
background-color: #368670;
font-family: sans-serif;
}
.ml13,
.ml14,
.ml15 {
font-size: 1.9em;
text-transform: uppercase;
letter-spacing: 0.2em;
font-weight: 600;
}
.ml15 {
letter-spacing: 0em;
text-align: center;
}
.ml13 .letter {
display: inline-block;
line-height: 1em;
}
.line {
display: flex;
margin: auto;
width: 50%;
margin-top: 500px;
}
.wrappy {
position: relative;
}
.wrappy h1 {
position: absolute;
left: 48.5%;
top: 20%
}
.butt {
padding-top: 50px;
display: flex;
margin: 0 auto;
width: 100%;
}
#btn1,
#btn2 {
display: table;
margin: 0 auto;
}
input {
z-index: 1000;
margin-left: 10%;
width: 80%;
background: transparent;
border: 0;
border-bottom: 1px solid;
padding: 1em 0 .1em;
text-align: center;
font-size: 18px;
font-family: inherit;
font-weight: 300;
line-height: 1.5;
color: inherit;
outline: none;
}
input:focus {
border-color: #ffffff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/2.0.2/anime.min.js"></script>
<div class="truth">
<!-- <div class="line"> -->
<div class="message-box">
<form class="message-form">
<h2 class="ml15" for="message">TELL A TRUTH</h2>
<div class="wrappy">
<input type="text" id="message" name="message" autocomplete="off" class="ml14">
<!-- <h1 id="test" class="ml13">I love your music!</h1> -->
<h1 id="test" class="ml13"></h1>
</div>
</form>
</div>
<div class="butt">
<button id="btn1" onclick="formCheck();">Ready to Send?</button>
<button id="btn2" style="display: none" onclick="myMove(); setTimeout(showButton, 3000); hideSendButton();">Send!</button>
</div>
</div>
You want to put H1 below the input.
Then you make the text input transparent. Bind the input value to h1.
So in effect when user clicks and type, they are selecting the input and changing its value, but it's transparent, to be shown by the h1 below the input that you will eventually animate.
Also because you mentioned you want it to display correctly in all platforms. You then have to be cognisant of the default behaviours of DOM and CSS properties. If you alter them to get what you want without knowing its natural order, you can get unexpected behaviour and reduce cross-browser compatibility. I have made changes to reflect that.
$(document).ready(function () {
$("#btn1").click(function () {
$("#test").text($("#message").val());
$("#message").val("");
$("#test").val("");
});
});
function onTextClick() {
document.getElementById("btn1").className = "show";
}
function showButton() {
document.getElementById("btn1").style.display = "block";
}
function showSendButton() {
document.getElementById("btn2").style.display = "block";
}
function formCheck() {
var input = $("#message").val();
if (input == "") {
alert("Please Submit a Valid Message");
} else {
hideButton();
showSendButton();
}
}
function hideButton() {
document.getElementById("btn1").style.display = "none";
}
function hideSendButton() {
document.getElementById("btn2").style.display = "none";
document.getElementById("sent").style.display = "block";
}
// attach this to bind h1 to the input value at all times.
$("#message").keyup(function () {
var self = this;
$("#test").text($(this).val());
});
function myMove() {
var textWrapper = document.querySelector(".ml13");
textWrapper.innerHTML = textWrapper.textContent.replace(
/\S/g,
"<span class='letter'>$&</span>"
);
anime.timeline().add({
targets: ".ml13 .letter",
translateY: [0, -1600],
opacity: [1, 0],
easing: "easeInSine",
duration: 3600,
delay: (el, i) => 800 + 60 * i
});
}
body {
background-color: #368670;
font-family: sans-serif;
}
.ml13,
.ml14,
.ml15 {
font-size: 1.9em;
text-transform: uppercase;
letter-spacing: 0.2em;
font-weight: 600;
}
.ml15 {
letter-spacing: 0em;
text-align: center;
}
.ml13 .letter {
display: inline-block;
line-height: 1em;
}
.line {
display: flex;
margin: auto;
width: 50%;
margin-top: 500px;
}
.wrappy {
position: relative;
text-align: center;
}
.wrappy h1 {
position: absolute; /* you then want to give wrappy h1 this to make it occupy no space. */
width: 100%; /* to centralize the text, your option here is to make this 100% width and use text-align */
text-align: center;
padding-top: 21px;
}
.butt {
padding-top: 50px;
display: flex;
margin: 0 auto;
width: 100%;
}
#btn1,
#btn2 {
display: table;
margin: 0 auto;
}
input {
position: relative; /* in order for z-index to work, you need to give an element `position` attribute of value `static`, `relative` or `absolute`. */
z-index: 1000; /* now this will work. wrappy h1 is not given a `z-index` so it defaults to `1`, hence input will be on top of wrappy h1 now. */
width: 80%;
background: transparent;
border: 0;
border-bottom: 1px solid #000; /* you need the line back because we are going to assign color to be transparent */
padding: 35px 0 0 0;
text-align: center;
font-size: 18px;
font-family: inherit;
font-weight: 300;
line-height: 1.5;
color: transparent; /* make the text transparent */
outline: none;
}
input:focus {
border-color: #ffffff;
}
<div class="truth">
<!-- <div class="line"> -->
<div class="message-box">
<form class="message-form">
<h2 class="ml15" for="message">TELL A TRUTH</h2>
<div class="wrappy">
<!-- for natural flow, you want to shift #test to above the input, so that input can stack on top of it -->
<h1 id="test" class="ml13"></h1>
<input type="text" id="message" name="message" autocomplete="off" class="ml14">
<!-- <h1 id="test" class="ml13">I love your music!</h1> -->
</div>
</form>
</div>
<div class="butt">
<button id="btn1" onclick="formCheck();">Ready to Send?</button>
<button id="btn2" style="display: none" onclick="myMove(); setTimeout(showButton, 3000); hideSendButton();">Send!</button>
</div>
</div>
I'm trying to implement a way to display / hide a div element with vanilla JavaScript triggered by a click event. The hide function works well but I seem to be missing something important when it comes to displaying the div's again. I've verified that the toggler function is working.
Simple sandbox here:
https://codepen.io/pen/eYmOzVe
(function() {
"use strict";
// HTML References
var flags = document.querySelector(".flags");
// Toogle
var toogle = true;
// Flag object
var flagObject = {
init: function(part1, part2, part3, part4, part5) {
this.part1 = part1;
this.part2 = part2;
this.part3 = part3;
this.part4 = part4;
this.part5 = part5;
},
draw: function() {
flags.innerHTML += `
<div id="${this.part1}">
<div class="${this.part2}">
<div class="${this.part3}"></div>
<div class="${this.part4}"></div>
<div class="${this.part5}"></div>
</div>
</div>
`;
},
toogler: function(arg) {
toogle ? flagObject.remove(arg) : flagObject.show(arg);
toogle = !toogle;
},
remove: function(arg) {
if (arg == "1") {
flag1Element.style.visibility = "hidden";
}
if (arg == "2") {
flag2Element.style.visibility = "hidden";
}
},
show: function(arg) {
if (arg == "1") {
flag1Element.style.visibility = "visible";
}
if (arg == "2") {
flag2Element.style.visibility = "visible";
}
}
};
// Create instances of the object
var swedishFlag = Object.create(flagObject);
var japaneseFlag = Object.create(flagObject);
// Init
swedishFlag.init(
"flag1",
"flag-sweden",
"cross-one-sweden",
"cross-two-sweden"
);
japaneseFlag.init("flag2", "flag-japan", "circle-japan");
// Array containing all flags
var allObjects = [swedishFlag, japaneseFlag];
// Draws flags
for (let i = 0; i < allObjects.length; i++) {
allObjects[i].draw();
}
// HTML element refrences
var flag1Element = document.querySelector("#flag1");
var flag2Element = document.querySelector("#flag2");
// Add eventlisteners to remove flags on click
flag1Element.addEventListener("click", function() {
flagObject.toogler(1);
});
flag2Element.addEventListener("click", function() {
flagObject.toogler(2);
});
})();
h1 {
text-align: center;
}
h3 {
color: green;
}
.content {
border: 1px solid black;
background-color: #eee;
padding: 2em;
margin: 0 auto;
height: 1000px;
width: 800px;
border-radius: 30px;
text-align: center;
}
.flags {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
height: 1000px;
}
.flag-sweden {
position: relative;
background-color: #006aa7;
height: 200px;
width: 320px;
margin-bottom: 2em;
}
.cross-one-sweden {
background-color: #fecc00;
position: absolute;
width: 40px;
height: 200px;
top: 0;
left: 100px;
}
.cross-two-sweden {
background-color: #fecc00;
position: absolute;
width: 320px;
height: 40px;
top: 80px;
left: 0;
}
.flag-japan {
position: relative;
height: 200px;
width: 320px;
background-color: white;
margin-bottom: 2em;
}
.circle-japan {
background-color: #bd0029;
height: 125px;
width: 125px;
border-radius: 50%;
position: absolute;
top: 50%;
left: 50%;
margin: -62.5px 0 0 -62.5px;
}
<h1>Sandbox</h1>
<div id="content" class="content">
<div class="flags"></div>
</div>
As Pavlin Petkov said in the comments, the image is not clickable when you hide it, so you can't toggle it back on. A simple solution to this that achieves the same result is to change the opacity instead of the visibility:
remove: function(arg) {
if (arg == "1") {
flag1Element.style.opacity = 0;
}
if (arg == "2") {
flag2Element.style.opacity = 0;
}
},
show: function(arg) {
if (arg == "1") {
flag1Element.style.opacity = 1;
}
if (arg == "2") {
flag2Element.style.opacity = 1;
}
}
This will display/hide a div with a click effect, and it will continue to occupy space on the page, as in your codepen. If you need to use visibility for some reason, I'd recommend a container div beneath the now hidden div which can trigger the show function; however, for the question at hand, this is sufficient.
Wrote some code to Toggle whether a Grid of Images is Displayed or not. it is a 6x8 grid, so i did not include the 72 div elements here. When the Image Grid is not showing, a text list is visible. Hence the change in Button Text. Having trouble Finding any Errors.
<!DOCTYPE html>
<html>
<head>
<style>
#photo-index {
display: grid;
grid-template-columns: auto auto auto auto auto auto;
grid-gap: 0px;
background-color: #4f6b6f;
}
#toggle-button {
color: white;
background-color: rgba(0,0,0,.75);
border-radius: 3px;
text-align: center;
padding: 15px;
margin: auto;
position: absolute;
right: 5%;
cursor: pointer;
}
</style>
<script>
function toggleDisplay() {
var pi = document.getElementById('photo-index');
var displaySetting = pi.style.display;
var toggle-button = document.getElementById('toggle-button');
var showTaxonomy = 'Show Taxonomy';
var showImages = 'Show Images';
if (displaySetting == 'none') {
pi.style.display = 'grid';
toggle-button.innerHTML = showImages;
}
else {
pi.style.display = 'none';
toggle-button.innerHTML = showTaxonomy;
}
}
</script>
</head>
<body>
<div onclick="toggleDisplay()" id="toggle-button">Show Taxonomy</div>
<div id="photo-index">Large Grid of Stuff</div>
</body>
</html>
There is a error in the variable declaration toggle-button is not a valid variable in JavaScript, so use toggle_button. The JavaScript variable do not allow hyphen in variable declaration as it is considered as arithmetic operator.
function toggleDisplay() {
var pi = document.getElementById('photo-index');
var displaySetting = pi.style.display;
var toggle_button = document.getElementById('toggle-button');
var showTaxonomy = 'Show Taxonomy';
var showImages = 'Show Images';
if (displaySetting == 'none') {
pi.style.display = 'grid';
toggle_button.innerHTML = showImages;
}
else {
pi.style.display = 'none';
toggle_button.innerHTML = showTaxonomy;
}
}
#photo-index {
display: grid;
grid-template-columns: auto auto auto auto auto auto;
grid-gap: 0px;
background-color: #4f6b6f;
}
#toggle-button {
color: white;
background-color: rgba(0,0,0,.75);
border-radius: 3px;
text-align: center;
padding: 15px;
margin: auto;
position: absolute;
right: 5%;
cursor: pointer;
}
<div onclick="toggleDisplay()" id="toggle-button">Show Taxonomy</div>
<div id="photo-index">Large Grid of Stuff</div>
I would please like an explanation to why the slideshow is not working. Below I have used an interval to perpetually change the slideshow, if userClick is false. The white and squared buttons (made of divs) are set to call upon two functions; slideRight() or slideLeft() and clicked(). When the buttons are clicked however, the clicked() function does not seem to change the variable, based on the data on top of the page.
<body>
<div class="page-wrapper">
<header>
<div class="headContent">
<h1 class="titleText">Slideshow</h1>
<h2 class="subTitleText">A slideshow made with JavaScript.</h2>
<p>userClick <span id="uc"></span></p>
</div>
<nav>
<ul>
<li>Home</li>
<li>About</li>
<li>Gallery</li>
</ul>
</nav>
</header>
<div class="body-wrapper">
<h1 class="titleText">Slideshow</h1>
<div id="slideshow">
<div id="leftSlide" onclick="leftSlide(); clicked()"></div>
<div id="rightSlide" onclick="rightSlide(); clicked()"></div>
</div>
<p>The image is not invoked by a tag, but invoked by the background property using Javascript.</p>
</div>
<footer>
<p id="footerText">© 2017 <br>Designed by JastineRay</p>
</footer>
</div>
<script language="javascript">
// Slide function
var slide = ["minivan", "lifeinthecity", "sunsetbodyoflove"];
var slideTo = 1;
window.onload = getSlide();
// Previous Image
function leftSlide() {
if (slideTo != 0) {
slideTo = slideTo - 1;
} else if (slideTo == 0) {
slideTo = slide.length - 1;
} else {
alert('SLIDE ERROR');
}
getSlide();
}
// Next Image
function rightSlide() {
if (slideTo != (slide.length - 1)) {
slideTo = slideTo + 1;
} else if (slideTo == (slide.length - 1)) {
slideTo = 0;
} else {
alert('SLIDE ERROR');
}
getSlide();
}
function getSlide() {
imageURL = 'url(images/' + slide[slideTo] + '.jpg)';
document.getElementById("slideshow").style.backgroundImage = imageURL;
}
// Interval Slideshow & Check if user clicked (timeout)
var userClick = false;
window.onload = slideInterval(5000);
// Start Slideshow
function slideInterval(interval) {
while (userClick = false) {
setInterval(function() {
rightSlide();
}, interval)
}
}
// Stop Slideshow and start timeout
function clicked() {
userClick = true;
setTimeout(function() {
userClick = false;
slideInterval();
}, 2000)
}
window.onload = function() {
setInterval(document.getElementById("uc").innerHTML = userClick), 100
}
</script>
</body>
CSS coding below.
* {
margin: 0;
padding: 0;
}
.page-wrapper {
width: 100%;
}
// Class Styling
.titleText {
font-family: monospace;
font-size: 40px;
}
.subTitleText {
font-family: monospace;
font-size: 20px;
font-weight: normal;
}
// Header Styling
header {
height: 500px;
}
.headContent {
margin: 30px 7%;
}
// Navigation Styling
nav {
overflow: hidden;
}
nav ul {
background: black;
background: linear-gradient(#595959, black);
list-style-type: none;
font-size: 0;
padding-left: 13.33%;
margin: 40px 0;
}
nav ul li {
padding: 15px 20px;
border-right: 1px solid #595959;
border-left: 1px solid #595959;
color: white;
display: inline-block;
font-size: 20px;
font-family: sans-serif;
}
// Body Styling
.body-wrapper {
}
.body-wrapper > .titleText {
text-align: center;
font-size: 50px;
}
#slideshow {
overflow: hidden;
margin: 20px auto;
border: 2px solid blue;
height: 350px;
max-width: 800px;
background-size: cover;
background-position: center;
position: relative;
}
#leftSlide {
position: absolute;
left: 40px;
top: 175px;
background-color: white;
height: 40px;
width: 40px;
}
#rightSlide {
position: absolute;
left: 100px;
top: 175px;
background-color: white;
height: 40px;
width: 40px;
}
// Footer Styling
Try changing the checking part to:
window.onload = function() {
setInterval(function () {
document.getElementById("uc").innerHTML = userClick;
}, 100);
}
The first argument of setInterval has to be a function (something that can be called), not a generic piece of code.
select drop down css
display: block;
height: 60px;
margin-left: 10%;
margin-right: 10%;
margin-top: 10px;
min-height: 60px;
text-align: center;
option text is centered in firefox browser,but not in safari,
Is there any solution to get text aligned in center in safari browser?
Add padding and remove the height. For example here is an update of the code you have supplied:
display: block;
margin-left: 10%;
margin-right: 10%;
margin-top: 10px;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
UPDATE
Unfortunately padding may not work in Safari (iPhone). In this case try using text indent instead of padding. And add the text indent of the width of your drop down menu. Please see an update of the code you provided:
display: block;
height: 60px;
margin-left: 10%;
margin-right: 10%;
margin-top: 10px;
min-height: 60px;
width: 220px; /* width of 200px plus the 20px for the text indent */
text-indent: 20px;
You could use top and bottom padding and remove height to align the text in the middle.
padding: 30px;
If you are desperate, use this one codepen Crossbrowser Select
$(document).ready(() => {
$('.crossbrowser-select').on('click', crossbrowserSelectOnClickEvent);
$('.crossbrowser-select').on('mouseWheelDown', crossbrowserSelectOnMouseWheelDownEvent);
$('.crossbrowser-select').on('mouseWheelUp', crossbrowserSelectOnMouseWheelUpEvent());
$('.crossbrowser-select > .option').on('click', crossbrowserSelectItemOnClickEvent);
$('.crossbrowser-select').focusout('click', crossbrowserSelectOnFocusOutEvent);
//Firefox
$('.crossbrowser-select').bind('DOMMouseScroll', function(e){
if (e.originalEvent.detail > 0) {
//scroll down
chooseNextItem($(this));
} else {
//scroll up
choosePreviousItem($(this));
}
//prevent page fom scrolling
return false;
});
//IE, Opera, Safari
$('.crossbrowser-select').bind('mousewheel', function(e){
if(e.originalEvent.wheelDelta < 0) {
//scroll down
chooseNextItem($(this));
}else {
//scroll up
choosePreviousItem($(this));
}
//prevent page fom scrolling
return false;
});
$('.crossbrowser-select').keyup(function(event) {
if (event.keyCode === 32) { //Enter
event.preventDefault();
toggleSelect($(this));
}
if (event.keyCode === 38) { //Up
event.preventDefault();
choosePreviousItem($(this));
}
if (event.keyCode === 40) { //Down
event.preventDefault();
chooseNextItem($(this));
}
});
});
/* METHODS ************************************************************************************************************/
function toggleSelect(select) {
if (select.hasClass('expanded')) {
select.toggleClass('expanded');
select.children('.option').removeClass('shown');
} else {
select.toggleClass('expanded');
select.children('.option').each(function () {
const item = $(this);
const select = item.parent();
if (item.attr('value') !== select.attr('value')) {
item.toggleClass('shown');
}
});
}
}
function collapseSelect(select) {
select.removeClass('expanded');
select.children('.option').each(function () {
$(this).removeClass('shown');
});
}
function chooseNextItem(select) {
collapseSelect(select);
if (select.attr('value') !== '') {
const selectedItem = select.children('div[value=\'' + select.attr('value') + '\']');
const nextSibling = selectedItem.next('.option');
if (nextSibling) {
select.attr('value', nextSibling.attr('value'));
select.children('.visual-option').html(nextSibling.html());
}
} else {
selectFirstItem(select);
}
}
function choosePreviousItem(select) {
collapseSelect(select);
if (select.attr('value') !== '') {
const selectedItem = select.children('div[value=\'' + select.attr('value') + '\']');
const prevSibling = selectedItem.prev('.option');
if (prevSibling) {
select.attr('value', prevSibling.attr('value'));
select.children('.visual-option').html(prevSibling.html());
}
} else {
selectFirstItem(select);
}
}
function chooseItem(item) {
const select = item.parent('.crossbrowser-select');
select.children('.visual-option').html(item.html());
select.attr('value', (item.attr('value')));
}
function selectFirstItem(select) {
const firstItem = select.children('.option').first();
select.attr('value', firstItem.attr('value'));
select.children('.visual-option').html(firstItem.html());
}
/* Events *************************************************************************************************************/
function crossbrowserSelectOnClickEvent() {
toggleSelect($(this));
}
function crossbrowserSelectItemOnClickEvent() {
chooseItem($(this));
}
function crossbrowserSelectOnMouseWheelDownEvent() {
chooseNextItem($(this));
}
function crossbrowserSelectOnMouseWheelUpEvent() {
choosePreviousItem($(this));
}
function crossbrowserSelectOnFocusOutEvent() {
collapseSelect($(this));
}
.crossbrowser-select {
text-align: center;
cursor: pointer;
border: 1px transparent solid;
border-bottom: 1px solid #70ccd9;
border-radius: 10px;
}
.crossbrowser-select.expanded {
color: #70ccd9;
border-top: 1px solid #70ccd9;
}
.crossbrowser-select:hover,
.crossbrowser-select:focus,
.crossbrowser-select:active {
outline: none;
border: 1px solid white;
background-color: transparent;
}
.crossbrowser-select > .option {
display: none;
color: white;
}
.crossbrowser-select > .option.shown {
display: block;
}
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body style="background-color: black; color: white; padding: 50px">
<div class="col-4 text-center">
<div style="margin-bottom: 20px">Previous random element</div>
<div value="" class="crossbrowser-select" tabindex="0">
<div class="visual-option">Select Item from list</div>
<div class="option" value="1option">Option1</div>
<div class="option" value="2option">Option2</div>
<div class="option" value="3option">Option3</div>
</div>
<div style="margin-top: 20px">Next random element</div>
</div>
<script src="https://code.jquery.com/jquery-1.12.3.js" ></script>
</body>
</html>