So, I wanted to display a loading gif when an input box was on focus.
I've managed to do it but the loading gif it's getting below the input box, and not after
On CSS, if I change the display:none to display:yes the loading gif appears after the input box as I want, but only until the JS function is triggered.
var input = document.getElementById('showloading');
var message = document.getElementsByClassName('loadingGif')[0];
input.addEventListener('focus', function() {
message.style.display = 'block';
});
input.addEventListener('focusout', function() {
message.style.display = 'none';
});
.loadingGif {
display: none;
}
<input id="showloading" type="text" class="auto">
<img class="loadingGif" src="loading.gif">
Screenshoots:
What it looks like:
What I want it to look like:
#T.J. Crowder aswer is the one you should follow if you want to use Javascript although you don't need Javascript to do what you want.
You can use only CSS by using :focus selector and the sibling selector + to write a style rule. When showloading is focused all the adjacent siblings with the class loadingGif will have the display: inline-block
Such as:
.loadingGif {
display: none;
}
#showloading:focus + .loadingGif {
display: inline-block;
}
<input id="showloading" type="text" class="auto">
<img class="loadingGif" src="loading.gif">
The problem is that block is displayed as a block, so it starts a new visual line.
I wouldn't use style at all, I'd use a class to show/hide the image:
var input = document.getElementById('showloading');
var message = document.getElementsByClassName('loadingGif')[0];
input.addEventListener('focus', function() {
message.classList.remove("hide"); // <===
});
input.addEventListener('focusout', function() {
message.classList.add("hide"); // <===
});
.loadingGif.hide {
/* --------^^^^^ */
display: none; /* <=== */
}
<input id="showloading" type="text" class="auto">
<img class="loadingGif hide" src="loading.gif">
<!-- ------------------^^^^ -->
...although as Pepper says (and Diogo also now says), you can do this with just CSS and without JavaScript or a class.
Related
I am trying to change the css of a html given a span class.
I have an html as follows:
<form id=thing>
<div id=a>
<span class=x>
</span>
</div>
<div id=b>
<span class=x>
</span>
</div>
</form>
When I inspect the webpage I see the following style being applied:
#thing .x {
display: none !important;
}
Now my goal is to get rid of this display css property completely.
I tried $("#thing .x").css("display", ""); but i keep getting "$(...).css is not a function - unable to remove css "
Any tips on how to solve this?
Using inline style with setAttribute()
I assume you are trying to remove the styling for display from none. '' => nothing would not be a valid style to use, use a valid property value recognized by the display property, like inline or block for example.
You can override the !important rule set in CSS with the same CSS hack used in inline style as inline style takes presidence over styles set in CSS sheet. The only way I was able to do it was by setting the style attribute using el.setAttribute('style', 'display: inline !important;'). See snippit and a few examples of failure and finally success below...
let dontUseImportant = document.querySelectorAll('#thing .x')
dontUseImportant.forEach(el => {
// style display inline !important is ignored
// this is likely a rule in JS that will disallow
// spaces in values passed into properties for style
// more research on this may be fruitful
// however note the outcome in console.log
el.style.display = "inline !important";
el.textContent = 'this will not parse to DOM'
console.log(el)
})
dontUseImportant.forEach(el => {
// style display is added but is over written by !important used in CSS
el.style.display = "inline";
el.textContent = 'this will not parse to DOM'
console.log(el)
})
// this also will not work
dontUseImportant.forEach(el => {
el.setAttribute('style', 'display: null !important;');
el.textContent = 'this will not parse to DOM'
console.log(el)
})
// by adding !important using setAttribute behind our
// property, this affects the elements style
// however, we are putting a bandaid on a bandaid
dontUseImportant.forEach(el => {
el.setAttribute('style', 'display: inline !important;');
el.textContent = 'content x is finally shown!'
console.log(el)
})
#thing .x {
display: none !important;
}
.y {
display: inline;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="thing">
<div id="a">content a
<span class="x">content x
</span>
</div>
<div id="b">content b
<span class="x">content x
</span>
</div>
</form>
Swapping classes on element
Because the CSS file is using !important this unfortunately will override any styles you place on the element using el.style.property = '' regardless of using JS or a JS library like JQuery.
You could add a new class using JS that has a defining display: inline and then remove the old class. Keep in mind that you must add a valid property value when using styles. display = '' is not valid.
Your particular issue is a great example as to why !important is a BAD IDEA for styling as a crutch!
let $dontUseImportant = $('#thing .x')
// iterate over elements and swap out the classes
// make sure to add the new class 'y' first
// then remove initial class 'x'
$.each($dontUseImportant, function(i){
$(this).addClass('y')
$(this).removeClass('x')
console.log($dontUseImportant[i])
})
#thing .x {
display: none !important;
}
.y {
display: inline;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="thing">
<div id="a">content a
<span class="x" >content x
</span>
</div>
<div id="b">content b
<span class="x">content x
</span>
</div>
</form>
Why not using the hidden attribute? It is supported in all modern browsers. So in your case, you don't even need CSS.
jQuery:
$('button').on('click', function() {
$('.x').each(function() {
$(this).show();
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="thing">
<div id="a">
<span class="x" hidden>
1
</span>
</div>
<div id="b">
<span class="x" hidden>
2
</span>
</div>
</form>
<button>Show</button>
Vanilla JS
const x = document.querySelectorAll('.x');
document.querySelector('button').addEventListener('click', (e) => {
for (let item of x) {
item.removeAttribute('hidden');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="thing">
<div id="a">
<span class="x" hidden>
1
</span>
</div>
<div id="b">
<span class="x" hidden>
2
</span>
</div>
</form>
<button>Show</button>
Not sure if this is possible but I'm trying to display a div if another div which doesn't share the same parent is hovered.
The html looks something like this:
<div class="test">
<div class="hover-me"><p>Hover</p></div>
</div>
// some other content here
<div class="hover-content">
<p>hovered content</p>
</div>
I've tried using
.test:hover + .hover-content {
display: block;
}
But I think this only works if there's no other content in-between? Any suggestions?
Use javascript to listen to the onmouseover event, or jquery to handle the hover event on one and change the display attribute of the other. Using jquery
<script>
$(document).ready(function () {
$(".hover-me").hover(function () {
$(".hover-content").show();
}, function() {
$(".hover-content").hide();
});
});
</script>
If you don't want to use jquery, change your html like so
<div class="test">
<div class="hover-me"
onmouseover="document.getElementById('hover-content').style.display = 'block';"
onmouseout="document.getElementById('hover-content').style.display = 'none';">
<p>Hover</p></div>
</div>
// some other content here
<div class="hover-content" id="hover-content">
<p>hovered content</p>
</div>
notice that I added an id attribute to the hover-content div.
Try this, i think it will help you :
<script>
$(document).ready(function () {
$( ".hover-me" ).mouseenter( function () {
$( ".hover-content" ).show();
}).mouseout(function () {
/*anything you want when mouse leaves the div*/
} );
});
</script>
So you want to display the .hover-content when you hover the test. You can try the following solution. If it does not work, you gotta use javascript to check for the mouseover event. Hope it helps!
.test:hover ~ .hover-content {
display: block;
}
HTML Element:
<p> <input type="button"> id="edit" value="edit page" /> </p>
This div has been hidden with display:none , I want to toggle the visibility to display:normal.
#Html.EditorFor(
modelItem => model.user,
new {
htmlAttributes = new {
# class = "user"
}
}
)
The class 'user' has the following properties:
.user {
width:200px;
margin-left:3px;
display:none;
}
For some reason my checked CSS isn't working:
#edit:hover + user {
color:black;
}
#edit:checked + user {
display:normal;
}
JSFiddle Example : https://jsfiddle.net/gp7pyssu/
I don't want to use any Javascript to toggle visibility, I'd like it to be done in pure CSS3.
Several CSS issues here:
display value
In CSS, there is no normal value for display.
Use block, inline or another value that fits your needs : http://www.w3.org/wiki/CSS/Properties/display
+ selector
To use the + selector in your CSS, you have to have your div just after your input, so you have to remove the p surrounding the input: http://www.w3.org/wiki/CSS/Selectors/combinators/adjacent
:checked selector
The selector :checked is only available for radio and checkbox input, you can't use it with a button input: http://www.w3.org/wiki/CSS/Selectors/pseudo-classes/:checked
With all that, you can check this working JSFiddle
<p> <input type="button" id="edit" value="edit page" /> </p>
#edit:checked + user {
display:block;
}
Have a look
I have a page which contains at the bottom 3 buttons with the following coding:
function printpage() {
//Get the print button and put it into a variable
var printButton = document.getElementById("printpagebutton");
//Set the print button visibility to 'hidden'
printButton.style.visibility = 'hidden';
//Print the page content
window.print()
printButton.style.visibility = 'visible';
}
#options {
align-content:center;
align-items:center;
text-align: center;
}
<div id="options">
<input type="submit" value="post news" >
<input id="printpagebutton" type="button" value="print news" onclick="printpage()"/>
<input type="button" value="re-enter the news">
</div>
I managed to hide the print button while printing but i couldn't with the others.
I've searched the internet for the solution, and most questions were answered by adding the display:none; in css, but i end up with 3 hidden buttons on the screen.
I only want the buttons hidden while printing
Answer might be simple, my knowledge in web developing is acient.
Thank you in advance.
You can use CSS #media queries. For instance:
#media print {
#printPageButton {
display: none;
}
}
<button id="printPageButton" onClick="window.print();">Print</button>
The styles defined within the #media print block will only be applied when printing the page. You can test it by clicking the print button in the snippet; you'll get a blank page.
You can use a css media query to target print:
#media print {
.hide-print {
display: none;
}
}
Assign an id to the other 2 buttons. For the POST NEWS button you can set id to postnews and RE-ENTER THE NEWS to reenterthenews; Then do this
function printpage() {
//Get the print button and put it into a variable
var printButton = document.getElementById("printpagebutton");
var postButton = document.getElementById("postnews");
var reenterButton = document.getElementById("reenterthenews");
//Set the button visibility to 'hidden'
printButton.style.visibility = 'hidden';
postButton.style.visibility = 'hidden';
reenterButton.style.visibility = 'hidden';
//Print the page content
window.print()
//Restore button visibility
printButton.style.visibility = 'visible';
postButton.style.visibility = 'visible';
reenterButton.style.visibility = 'visible';
}
HTML
<div id="options">
<input type="submit" id="postnews" value="post news" >
<input id="printpagebutton" type="button" value="print news" onclick="printpage()"/>
<input type="button" id="reenterthenews" value="re-enter the news">
</div>
I have a text field and an explaining div. Can I make this explaining div have opacity = 0 ONLY when I type something in the text field?
Thanks in advance for any help.
Here is the HTML code:
<input type='text' name='input' id="searchdevice" class="search-field" placeholder="" autofocus/>
<div id="explain">
Search your device in the text field
</div>
You can do it with just CSS if you set the input as required:
<input type='text' name='input' id='searchdevice' class='search-field' required='required' autofocus />
<div id='explain'>
Search your device in the text field
</div>
CSS:
/* Show by default */
#explain {
opacity: 1;
}
/* Hide it when input field has content */
#searchdevice:valid + #explain {
opacity: 0;
}
/* Remove "invalid" styling when input field is empty.
E.g. in Firefox, the input has a red box-shadow by default. */
#searchdevice:invalid {
box-shadow: none;
}
When you type something in the input field, it's "valid" and the #explain will have opacity of 0.
Browser support for the :valid selector: http://caniuse.com/#feat=form-validation
Demo: https://jsfiddle.net/2ozh40vp/1/
This will require JavaScript to listen to text input and hide the DIV.
Example using jQuery:
$('#searchdevice').on('input', function(){
$('#explain').addClass('hidden');
});
css:
.hidden {
opacity: 0;
}
Working fiddle: http://jsfiddle.net/spanndemic/kphgg2d0/
You can try:
$('#searchdevice').on('input', function(){
$('#explain').css('opacity', 0);
});
You only need this css line:
input:focus + #explain{opacity:0}
http://jsfiddle.net/kLLkyvyh/
Yes, javascript is good here though, functionality is kept where it belongs, responding to events in css is questionable practice. You'll want the keypress event for typing. The functions defined separately makes them easier to re-use.
var hideExplain = function() {
document.getElementById('explain').style.opacity='0';
}
document.getElementById('searchdevice').addEventListener("keypress", hideExplain);
see keypress example here
You might be better doing this though, as focus and blur will allow you to undo the effect when the user moves on. There's a show function included here too.
var showExplain = function() {
document.getElementById('explain').style.opacity='1';
}
document.getElementById('searchdevice').addEventListener("focus", hideExplain);
document.getElementById('searchdevice').addEventListener("blur", showExplain);
see the example here
You could use keypress to remove the tip and blur to reshow it, that way the tip would hang around for as long as possible for the user. See anothe example
Also, you would find it better to add and remove classes - here's an example with JQuery. Now your style classes are re-usable too.
CSS
.is-transparent {
opacity: 0;
}
.is-opaque {
opacity: 1;
}
JQuery
$('#explain').removeClass('is-opaque').addClass('is-transparent');
You can use this code:
<html>
<head>
<title>Some title</title>
<script src="http://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type='text/javascript'>
$(document).ready(function(){
$('#searchdevice').blur(function(){
$('#explain').fadeTo(1000, 0);
});
});
</script>
</head>
<body>
<input type='text' name='input' id="searchdevice" class="search-field" placeholder="" autofocus/>
<div id="explain">Search your device in the text field</div>
</body>
</html>
Here you can try various effects through fadeto from the link - http://api.jquery.com/fadeTo/