How to make DIV constantly display text on click? - html

I want a way (ideally just using CSS HTML) where when I click on Question, the text shows and remains there. Then if they click on text again, it will revert back to question.
It works on hover, but I don't know how to make it on click. HTML:
<div id="packagequestioninfo">
<p class="packagereplies">Question</p>
<p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>
CSS:
#packagequestioninfo {
padding: 10px;
background: #F2F7FA;
border-radius: 0px;
-moz-box-shadow: 0 0 5px #ccc;
-webkit-box-shadow: 0 0 5px #ccc;
box-shadow: 0 0 5px #ccc;
}
#packagequestioninfo:hover {
background-image:url(../img/index/body/ourproducts/light_blue_background_pattern.jpg);
background-repeat:repeat;
cursor: none;
}
#packagequestioninfo .packagecomment {
display: none;
}
#packagequestioninfo:hover .packagereplies {
display: none;
}
#packagequestioninfo:hover .packagecomment {
display: inline;
}
Here is the code http://jsfiddle.net/53UK2/

Make .packagecomment invinsible with css, then use jQuery:
$('p').click(function(){
$('p').toggle();
});
Fiddle: http://fiddle.jshell.net/RcTGN/

If you do not need IE8 and lower support you can do it with pure CSS like this.
This is because of the ":checked" css selector. See support here.
The HTML:
<div class="question">
<input type="checkbox" class="qestion-checkbox" id="q1" />
<label for="q1" class="qestion-text">Question 1 text</label>
<label for="q1" class="qestion-answer">Question 1 answer</label>
</div>
<div class="question">
<input type="checkbox" class="qestion-checkbox" id="q2" />
<label for="q2" class="qestion-text">Question 2 text</label>
<label for="q2" class="qestion-answer">Question 2 answer</label>
</div>
The CSS:
.qestion-answer, .qestion-checkbox {
display: none;
}
.qestion-checkbox:checked + .qestion-text {
display:none;
}
.qestion-checkbox:checked + .qestion-text + .qestion-answer {
display:block;
}
If you do need IE8 and lower support you need to use some javascript/jQuery

It's impossible using only css. Of course you can change :hover instead :active, but it will be work when the mouse button is held down.
Check it.
Why don't you want use jquery? It will be easier.

Non-jquery way...
Just showing and hiding by changing the display style attribute:
HTML:
<script src="javascriptfile.js"></script>
<div id="packagequestioninfo">
<p class="packagereplies">Question</p>
<p class="packagecomment">If you require <b>hosting</b> for your website, select your primary website and go to <b>Part II</b>. If you do not require hosting, please Checkout below. </p>
</div>
javascriptfile.js:
// Wait for the entire window elements to be loaded
window.onload = function() {
document.addEventListener('packagequestioninfo').addEventListener('onclick', function() {
if(document.getElementById('packagecomment').style.display !== 'none') {
document.getElementById('packagecomment').style.display = 'none';
} else {
document.getElementById('packagecomment').style.display = 'block';
}
});
}
or if you'd like to go with the class toggling method:
window.onload = function() {
document.addEventListener('packagequestioninfo').addEventListener('onclick', function() {
if(document.getElementById('packagecomment').className.indexOf('show')) {
document.getElementById('packagecomment').className = '';
} else {
document.getElementById('packagecomment').className = 'show';
}
});
}
in this approach you'll need to add a class to your CSS:
.show{
display:block;
}

Related

how to push [read more] button twice

I want to shrink my paragraph into a couple of read more, without the need to read less.
I only manage to do it once and 'm now lost.
.bio {
font-family: monospace;
}
#check {
display: none;
}
#check:checked~.more {
display: block;
}
.more {
display: none;
}
label {
color: rgb(0, 0, 0);
cursor: pointer;
font-weight: bold;
text-decoration: underline;
}
<div class="bio">
<input type="checkbox" id="check">
<p>craftsman</p> *original display*
<div class="more">
<p>since birth has always had a love/hate relationship</p> *first readmore*
</div>
<label for="check"> more</label>
<div class="more">
<p>She had half-survived blahblahblah.</p> *second readmore*
</div>
</div>
Make all elements with the class more hidden by using: .more { display: none }
Create a class to make elements visible again: .d-block { display: block }
Create a Node List in JS with all elements with the class more: let bio_sections = document.querySelectorAll('.more')
Create a variable at 0 as index: let bio_index = 0
Add a click-eventListener linked to the button: button-element.addEventListener('click', () => { ... })
Within the function create an if-condition to ensure that the actual function is only as often run as you have elements with the class more as otherwise, you will create an error after every further button click: if (bio_index < bio_sections.length)
select the element out of the Node List with the current index: bio_sections[bio_index]
Add that previously created class to that element: .classList.add('d-block')
Increase the index to jump to the next element within your Node List: bio_index++
let button = document.querySelector('.bio button');
let bio_sections = document.querySelectorAll('.bio .more');
/* index to iterate */
let bio_index = 0;
//trigger to check for button clicks
button.addEventListener('click', function() {
// ensures JS errors as otherwise elements going to be called that does not exist
if (bio_index < bio_sections.length) {
// makes the section visible
bio_sections[bio_index].classList.add('d-block');
//increases the index onclick
bio_index++;
}
})
.more {
display: none;
}
.d-block {
display: block;
}
button {
display: block;
margin-top: 2em;
}
<div class="bio">
<p>craftsman</p> *original display*
<div class="more">
<p>since birth has always had a love/hate relationship</p> *first readmore*
</div>
<div class="more">
<p>She had half-survived blahblahblah.</p> *second readmore*
</div>
<button>Read more about craftsman</button>
</div>

press a button to a css div block

I am working on a website.
I have a div block
<div id="table_and_dqinfo" class="table_and_dqinfo">
<div id="dqdiv" class="dqinfo">
<h4 class="centertext">Information Pane</h4>
<div id="dqdiv_volgraph"></div>
</div>
</div>
dqdiv_volgraph is an image block which has the image link.
Instead of putting the div block above into the CSS to show the image directly on the website, I want to put a CSS button, so that only after I press the button, it would open the div block above to show the image on the website.
Could anyone give me some hints about how to do it?
Is this what u want?
I used checkbox and label as a button.
When you click on label, it checks the checkbox, then using sibling selector (+) of CSS
input:checked + #dqdiv_volgraph{
display:block;
}
I toggle the visibility of the #dqdiv_volgraph div
Feel free to style the label tag and make it look like a button according to your project
Note that the for attrib of label and id of the input should
be same
input,
#dqdiv_volgraph {
display: none;
}
label {
border: 1px solid black;
padding: 5px;
cursor: pointer;
}
input:checked+#dqdiv_volgraph {
display: block;
}
<div id="table_and_dqinfo" class="table_and_dqinfo">
<div id="dqdiv" class="dqinfo">
<h4 class="centertext">Information Pane</h4>
<input id="checkb" type="checkbox">
<div id="dqdiv_volgraph">
<h1>Put image here</h1>
<img src="https://placehold.it/200x200">
</div>
<label for="checkb"> Click me</label>
</div>
</div>
Alternatively, you could also use JavaScript to solve this particular issue.
function showImage(n) {
var Images = document.getElementsByClassName("table_and_dqinfo");
//Get all elements with class "table_and_dqinfo"
var i = 0;
if (Images[n].style.display == "block") {
Images[n].style.display = "none";
return;
}
//Checks to see if the selected Image is already visible. If so it
//switches it makes it invisible
for (i = 0; i < Images.length; i++) {
Images[i].style.display = "none";
}
//Make the other Images invisible.
Images[n].style.display = "block";
//Change selected Image to visible
}
function showAll() {
var Images = document.getElementsByClassName("table_and_dqinfo");
var i = 0;
for(i = 0; i < Images.length; i++) {
if (Images[i].style.display == "block") {
Images[i].style.display = "none";
continue;
}
Images[i].style.display = "block";
}
}
.table_and_dqinfo {
display: none;
background-color: blue;
color: white;
}
.button {
border: 1px solid black;
cursor: pointer;
margin: 20px;
}
<span class="button" onclick="showImage(0)"> Button for Image 1 </span>
<span class="button" onclick="showImage(1)"> Button for Image 2 </span>
<span class="button" onclick="showAll()"> Button to show all Images </span>
<div class="table_and_dqinfo">
<div>
<h4>Image 1</h4>
<div></div>
</div>
</div>
<div class="table_and_dqinfo">
<div>
<h4>Image 2</h4>
<div></div>
</div>
</div>
Style the button as you please. You can also put whatever you want inside the <div class="table_and_dqinfo">

In HTML can I disable user interaction with an entire DOM sub-tree?

In HTML can I disable user interaction with an entire DOM sub-tree via CSS?
Use CSS pointer-events property on an element to disable user interaction on it and its descendants.
div.disabled { pointer-events: none; }
You could also use the user-select property to further restrict the user interaction of text selection.
div.disabled { user-select: none; }
Note that the user-select property may need vendor prefixes.
However, these CSS properties will not disable keyboard interactions which could happen by way of tabbing into the descendants.
As per this reference -- https://html.spec.whatwg.org/multipage/interaction.html#inert-subtrees; there is a proposal of an inert property but it is not yet implemented by browsers.
So, as of now you will resort to Javascript to disable keyboard interactions. Just wire up the keydown event with capture and set the returnValue to false. Do not forget to allow Tab key to allow for an escape out, otherwise the focus could get trapped.
var noInteracts = document.getElementsByClassName('disabled');
[].map.call(noInteracts, function(elem) {
elem.addEventListener("keydown", function(e) {
if (e.keyCode != 9) { // allow tab key to escape out
e.returnValue = false;
return false;
}
}, true);
});
You could also hide the highlight-outline on focus of inputs by this CSS:
div.disabled *:focus { outline: 0; }
Below is a demo with all of the above techniques combined.
Demo fiddle: http://jsfiddle.net/abhitalks/hpowhh5c/5/
Snippet:
var noInteracts = document.getElementsByClassName('disabled');
[].map.call(noInteracts, function(elem) {
elem.addEventListener("keydown", function(e) {
if (e.keyCode != 9) {
e.returnValue = false;
return false;
}
}, true);
});
div.disabled {
pointer-events: none;
-webkit-user-select: none; -moz-user-select: none; -ms-user-select: none;
user-select: none;
}
div.disabled *:focus { outline: 0; }
<p>This is normal</p>
<form>
<label>Input 1: <input id="i1" /> </label>
<button>Submit</button>
</form>
<hr />
<div class="disabled">
<p>User interaction is disbled here</p>
<form>
<label>Input 2: <input id="i2" /> </label>
<button>Submit</button>
</form>
<p>Lorem ipsum</p>
</div>
If you need to disable all types of interaction across the whole page, you can simply put a transparent div over everything:
#mask {
position: fixed;
left: 0;
top: 0;
z-index: 10; /* some high z-index */
width: 100vw;
height: 100vh;
opacity: 0;
user-select: none; /* prevents double clicking from highlighting entire page */
}
<!DOCTYPE html>
<html>
<body>
<input type="text">
<button>button</button>
link
<div id="mask"></div>
</body>
</html>

Is there a way where I can highlight the field/div of the focused input?

<!doctype html>
<html>
<style>
input:focus {
background: yellow;
}
div:focus {
background: gray;
}
</style>
**This section must be highlighted whenever i run the code**
<div>
SOME TEXT<input type = 'text' autofocus>
</div>
<div>
SOME TEXT <input type = 'text'>
</div>
</html>
Hi i tried something using jQuery, though there may be other ways to reach this result.
First i've made a class .gray which i use in the jquery part. And if you use this be sure to include <script src="http://code.jquery.com/jquery-1.11.2.min.js"></script>
CSS
input:focus {
background: yellow;
}
.gray {
background:gray;
}
jQuery
$('input').blur(function(){
$('input').parent().removeClass("gray");
})
.focus(function() {
$(this).parent().addClass("gray")
});
jsFiddle
If you want you can replace the remove/addClass by css('background', 'gray');
note this may not be supported in older browsers especially older versions of IE (below 10)
input:focus {
background: yellow;
}
.gray {
background: gray;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('input').blur(function() {
$('input').parent().removeClass("gray");
})
.focus(function() {
$(this).parent().addClass("gray")
});
});
</script>
<div>
SOME TEXT
<input type="text" value="" autofocus>
</div>
<div>
SOME TEXT
<input type="text" value="">
</div

HTML select with items shown horizontally

Good morning,
I'm developing a web application and I need an horizontal input that lets the user select a value, but radio buttons are not an option.
I thought about using a input with range type, but this is not supported in IE 8 or 9.
Is there any way to show select options horizontally that works both in chrome and IE?
My html code is:
<select size="4">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="audi">Audi</option>
</select>
It currently looks like this:
Is there any way to make the options show side by side? I tried adding inline-block display to the options, but nothing changes.
--- EDIT ---
I don't want to use radio buttons because I'm using js to create new items which the user can also select and with radio buttons I have to change the button names manually.
Is this what you're after?
select {
display:table-row;
}
option {
display:table-cell;
}
http://jsfiddle.net/rjmnmcru/
Your best bet is probably to use either checkboxes, or an unordered list with links:
<style type="text/css">
ul.selections { list-style:none; }
ul.selections li {display: inline; list-style-type: none; padding-right: 20px;}
div.selections input { margin-left:15px;}
div.selections input.first { margin-left:20px; }
</style>
<div class="selections">
<input type="checkbox" value="volvo" class="first"/> Volvo
<input type="checkbox" value="saab"/> Saab
<input type="checkbox" value="mercedes"/> Mercedes
<input type="checkbox" value="audi"/> Audi
</div>
<ul class="selections" >
<li>Volvo</li>
<li>Saab</li>
<li>Mercedes</li>
<li>Audi</li>
</ul>
you can use the links or onchecked of the checkboxes to fire JS events. To make it clear to the end user, you can add a "highlight" class to the elements from the JS event, and change the background colors as necessary.
I realize you've already accepted an answer, however this works too.
You can customize anything you want in it, including borders, background colours, etc.
HTML
<div class="container"><span class="user">User 1</span>
<div class="selectOptions">
<div class="myOptions">
<div class="option volvo">Volvo</div>
<div class="option saab">Saab</div>
<div class="option mercedes">Mercedes</div>
<div class="option audi">Audi</div>
</div>
<div class="select user1">User 1</div>
</div>
</div>
CSS
.container {
overflow:hidden;
background-color:#ddd;
padding:5px;
padding-bottom:7px;
}
.user {
float:left;
background-color:#ddd;
margin-top:2px;
padding:5px;
}
.user:after {
content:":";
}
.selectOptions {
float:left;
margin-left:5px;
}
.select {
border:2px solid #333;
padding:5px;
text-align:center;
position:absolute;
background-color:#ddd;
}
.select:hover {
background-color:grey;
cursor:pointer;
}
.myOptions {
position:absolute;
top:45px;
/*42px (height of .select + (padding*2) minus border thickness*/
border:2px solid #333;
display:none;
background-color:#ddd;
}
.option {
display:inline-block;
padding:5px;
}
.option:not(:first-of-type) {
margin-left:-5px;
}
.option:hover {
background-color:#123456;
cursor:pointer;
}
JS
$(".select").hover(function () {
$(this).siblings(".myOptions").css("display", "block");
$(this).css("background-color", "grey");
}, function () {
$(this).siblings(".myOptions").css("display", "none");
});
$(".myOptions").hover(function () {
$(this).css("display", "block");
}, function () {
$(this).css("display", "none");
$(this).siblings(".select").css("background-color", "#ddd");
});
$(".option").hover(function () {
$(this).css("background-color", "#123456");
}, function () {
$(this).css("background-color", "#ddd");
var selection = $(this).html();
if (selection == $(this).parent().siblings(".select").html()) $(this).css("background-color", "grey");
});
$(".option").click(function () {
var selection = $(this).html();
$(this).parent().siblings(".select").html(selection);
$(this).siblings().css("background-color", "#ddd");
$(this).css("background-color", "grey");
});