div content shows when display:none - html

I have a couple of divs that I only show after a certain action. Like a message for validation. For this one there are a couple of messages in one div, and I want them to be a couple of pixels apart. I know how to do it, but when I change the css you can see a part of the content without clicking the submit button.
With this code everything is okay, except I want the messages to be some pixels apart (padding-top: 5px or something) but when I do that you always see the padding I added, even on display: none.
My div:
<div id="fout"></div>
</div>
Style:
#fout
{
display:none;
margin-left:-75px;
display:block;
font-size:15px;
background-color:#D73033;
width:350px;
font-family:"Myriad Pro";
color:#FFFFFF;
text-align:center;
border-radius:2px;
}
Here is my jquery just in case you need it:
$(function(){
// initialisatie formulier validatie. (validate.min.js)
var validator = new FormValidator('form', [{
name: 'voornaam',
display: 'Voornaam',
rules: 'required'
}, {
name: 'achternaam',
display: 'achternaam',
rules: 'required'
},{
name: 'telefoonnummer',
display: 'telefoon',
rules: 'required|numeric'
},{
name: 'email',
display: 'email',
rules: 'required|valid_email'
}], function(errors, event) {
var berichten = document.getElementById('fout');
$("#fout").css("display","none");
$("#fout").fadeIn('slow').css;
berichten.innerHTML = '';
// als er fouten zijn:
if (errors.length > 0) {
for (var i = 0, l = errors.length; i < l; i++) {
berichten.innerHTML += errors[i].message + '<br>';
}
// als de validatie goed gegaan is:
} else {
var voornaam = $('#voornaam').val();
var achternaam = $('#achternaam').val();
var telefoonnummer = $('#telefoonnummer').val();
var email = $('#email').val();
$.post('action.php',{action: "button", voornaam:voornaam, achternaam:achternaam, telefoonnummer:telefoonnummer, email:email},function(res){
$('#result').html(res);
});
$('#insert').remove();
$("#formpje").hide('slow').css;
document.getElementById('goed').innerHTML = 'Verstuurd!';
}
// voorkom ten allertijde dat het formulier daadwerkelijk ge-submit wordt!
event.preventDefault();
});
});

you have set :
display:none;
margin-left:-75px;
display:block;
so display:block; overwrite display:none;
Remove display:block;
#fout
{
display:none; /* <-- 1st declaration */
margin-left:-75px;
display:block; /* <-- 2nd declaration which overwrites 1st declaration */
font-size:15px;
background-color:#D73033;
width:350px;
font-family:"Myriad Pro";
color:#FFFFFF;
text-align:center;
border-radius:2px;
}
EDIT
As mentioned in the comments by abhitalks
this is also a problem :
$("#fout").css("display","none"); $("#fout").fadeIn('slow').css;
EDIT 2
to give space between the lines, give :
line-height:2em; in your #fout class!!
demo here

Related

How to Search text from input field to textarea, and How to marked matched result Using Jquery?

I am trying to create simple jquery App, but i ran over small problem, which i can't solve, I want to search in textarea box form input field, if input form value will match textarea value, i want to marked matched text, picture below shows what i want to done, any suggestion?
$('#search').on('input', function(e) {
e.preventDefault();
var searchTxtBox = $('#search');
searchTxtBox.val(searchTxtBox.val().replace(/(\s+)/, "(<[^> ]+>)*$1(<[^> ]+>)*"));
var textarea = $('#editor');
var enew = '';
if (searchTxtBox.val() != '') {
enew = textarea.html().replace(/(<mark>|<\/mark> )/igm, "");
textarea.html(enew);
var query = new RegExp("(" + searchTxtBox.val() + ")", "gim");
newtext = textarea.html().replace(query, "<mark>$1</mark>");
newtext = newtext.replace(/(<mark>[^<>]*)((<[^> ]+>)+)([^<>]*<\/mark>)/, "</mark><mark>");
textarea.html(newtext);
} else {
enew = textarea.html().replace(/(<mark>|<\/mark> )/igm, " ");
textarea.html(enew);
}
});
mark {
background-color: red;
color: black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input placeholder='search' id='search'>
<div id="editor" rows="4" cols="50">
</div>
The simplest way I can think of is positioning a transparent element behind the textarea
and adding the results there:
Key features to program this:
The elements must render the same (size, font, line ect..)
The textarea must have a transparent background.
Whenever the textarea is changed (resized) we need to match the size of the result container.
Here is my implementation:
$(function(){
//Simple helper function to match the size of the elements:
function matchSize($base, $target) {
$target.css({
width : $base.outerWidth(),
height : $base.outerHeight()
});
}
//Attach whenever serach field changed run the query:
$("#search").keyup(function(){
let $search = $(this),
$input = $('#input');
let $result = $input.prev('code');
//Match size:
matchSize($input, $result)
//Search
let marked = $input.val().replace(
new RegExp("(" + $search.val().trim() + ")", "g"),
"<mark>$1</mark>"
);
//Set marked transparent text:
$result.html(marked);
});
//textarea can be resized so always match the size:
$('#input').bind('mouseup', function(){
let $input = $('#input');
let $result = $input.prev('code');
matchSize($input, $result)
});
});
.wrap {
position: relative;
display:block;
}
/* match the two */
.wrap code,
.wrap textarea {
position:absolute;
text-rendering: auto;
letter-spacing: normal;
word-spacing: normal;
text-transform: none;
text-indent: 0px;
text-align: start;
appearance: textarea;
flex-direction: column;
white-space: pre-wrap;
overflow-wrap: break-word;
margin: 0em;
font: 400 13.3333px Arial;
border-width: 1px;
border-style: solid;
padding: 2px;
background-color:transparent;
z-index: 2;
box-sizing: border-box;
}
.wrap code {
z-index: 1;
top:0;
left:0;
border-color:transparent;
color:transparent;
background-color: white;
}
.wrap code mark {
color: transparent;
background-color: yellow;
padding:0;
margin:0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="wrap">
<code></code>
<textarea id="input">the easiest way to do this and the quickest.</textarea>
</div>
<br/><br/><br/><br/>
Search: <input id="search" placeholder='Search' />

Absolute positioning bug in display:table-cell in IE 11

Compare and contrast this JSfiddle in Chome (correct) and IE11 (very definitely not correct):
http://jsfiddle.net/Lr90ko3q/
IE seems to see the height of the CONTENT in the inner div, rather than the actual height of the div. In other words, if you add a couple more lines of content, the left/right arrows move down a bit.
Is this a known bug, and is there a usable workaround for it?
Thanks
(copy of JSfiddle code):
html {
box-sizing:border-box
}
*, *::before, *::after {
box-sizing:inherit
}
html, body {
position:relative;
width:100%;
height:100%
}
section {
width:60%;
height:60%;
position:relative;
background:rgba(0, 0, 0, 0.1)
}
section>.content {
display:table;
position:absolute;
top:0;
left:0;
width:100%;
height:100%;
padding:50px
}
section>.content>div {
display:table-cell;
vertical-align:middle;
position:relative;
background:rgba(255, 0, 0, 0.2);
text-align:center
}
#left {
position:absolute;
left:5%;
top:50%;
margin:0;
transform:translateY(-50%)
}
#right {
position:absolute;
right:5%;
top:50%;
margin:0;
transform:translateY(-50%)
}
I fixed the issue by just adding JS that forces a fixed height on the table cell elements.
Also used native JS since the functionality is fairly simple and supported on each of the browsers.
Obviously replace .table/.table-cell with whatever you called your table/table-cell items that you're targeting.
"use strict";
var ua = navigator.userAgent.toLowerCase();
var ie_version = (ua.indexOf('msie') != -1) ? parseInt(ua.split('msie')[1]) : false;
if (!ie_version && ua.indexOf("trident") !== -1 && ua.indexOf("rv:11") !== -1)
{
ie_version = 11;
}
if (ie_version == 9 || ie_version == 10 || ie_version == 11)
{
(function(){
window.onresize = calculateTableCellHeights;
calculateTableCellHeights();
function calculateTableCellHeights() {
// Fixes IE9/10/11 bug where table-cell doesn't inherit table height
var tables = document.querySelectorAll(".table");
for (var t = 0; t < tables.length; ++t)
{
var table = tables[t];
var table_cells = table.querySelectorAll(".table-cell");
for (var c = 0; c < table_cells.length; ++c)
{
var table_cell = table_cells[c];
var display = window.getComputedStyle(table_cell, null).getPropertyValue("display");
if (display == "table-cell")
{
// Set fixed height
table_cell.style.height = table.offsetHeight+"px";
}
else
{
// If item is no longer a table-cell due to responsive stacking
// remove the height.
table_cell.style.height = "";
}
}
}
}
})();
}

How to hide part of css when input is empty?

I'm trying to hide the search result when the search is back to empty. Is there a way to do that?
Everything is working fine. I just don't like to see the search result of the last letter. Example if you type A and you empty the input box. The result will stay there.
Thanks to the person that know the answer.
<div id="result"></div> should be the one to be blocked.
My CSS code for the search:
.content{
width:275px;
}
#searchid
{
width:275px;
border:solid 1px #000;
padding:10px;
font-size:14px;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
#result
{
position: absolute;
width:275px;
padding:10px;
display:none;
border-top:0px;
overflow:hidden;
border:1px #CCC solid;
background-color: white;
}
.show
{
padding:10px;
border-bottom:1px #999 dashed;
font-size:15px;
height:50px;
}
.show:hover
{
background:lightgrey;
color:#FFF;
cursor:pointer;
}
My HTML Code for the search input box:
<div class="content">
<input type="text" class="search" id="searchid" placeholder="Search"/>
<div id="result"></div>
</div>
My Search Display are like that:
<div class="show" align="left">
<span class="name">' . $rowsTitle['title'] . ' ' . $label . "<br>".
</div>
JQuery that I use to get the search information:
$(function(){
$(".search").keyup(function()
{
var searchid = $(this).val();
var dataString = 'search='+ searchid;
if(searchid!='')
{
$.ajax({
type: "GET",
url: "/sheridan/script/search.php",
data: dataString,
cache: false,
success: function(html)
{
$("#result").html(html).show();
}
});
}return false;
});
jQuery("#result").live("click",function(e){
var $clicked = $(e.target);
var $name = $clicked.find('.name').html();
var decoded = $("<div/>").html($name).text();
$('#searchid').val(decoded);
});
jQuery(document).live("click", function(e) {
var $clicked = $(e.target);
if (! $clicked.hasClass("search")){
jQuery("#result").fadeOut();
}
});
$('#searchid').click(function(){
jQuery("#result").fadeIn();
});
});
I forgot to add the JQuery function that I use to get the information out of the database.
I haven't cleared the result div as you are filling it with data when there is a letter.
$(".search").on("keyup", function(){
if($(this).val()==""){
$("#result").hide();
}
else {
$("#result").show();
}
});
Fiddle
You will have to use JavaScript to perform this action as CSS does not support any way to identify empty input fields.
I have chosen to use jQuery for this example but it could easily be achieved using no external libraries.
$('.search').on('input', function() {
$('#result').toggle( $(this).val() );
});
JSFiddle
Use JQuery
$( "#searchid" ).keypress(function() {
var search = $( "#searchid" ).val()
if(search==''){
$("#result").html();
}
});
Try following code:
$(document).ready(function(){
$("#searchid").keypress(function(){
var len = $("#searchid").val().length;
if (len <= 0)
{
//make result blank
$("#result").hide();
}
});
});
edit:
use $("#result").hide();
or
$("#result").css("display","none");

How to disable my element auto-scroll briefly

I used the answer to this question to auto-scroll a combat log for my HTML5 game to the bottom:
How to auto-scroll to end of div when data is added?
If I want to briefly disable this while a person clicks to scroll up and view the log, how would I do so?
The Code I'm using is:
//autoscroll combat log
window.setInterval(function() {
var elem = document.getElementById('log');
elem.scrollTop = elem.scrollHeight;
}, 500);
Best regards,
Cobwebs
Edit: the code above is connected to a simple span in a div:
<div>
<span id='log'></span>
</div>
Stylized to have a scrollbar:
span#log {
display: block;
font-family: courier, sans;
margin: auto;
border:1px solid black;
width:600px;
height:200px;
overflow:auto;
background-color:black;
color: lime;
text-align: left;
}
setInterval() returns an ID, with that ID you can use clearInterval() to stop it
var intervalID = window.setInterval(function() {
var elem = document.getElementById('log');
elem.scrollTop = elem.scrollHeight;
}, 500);
and then this
window.clearInterval(intervalID);
small DEMO

Firefox no transition when fading back in an image

I made a nice transition effect when switching between images, it sets the opacity to 0 (With a transition of all .5s) then .5s later (Via setTimeout) the src of the image is changed and it should fade back in.
It works fine on Chrome and IE but on firefox the image fades out and then instead of fading back to opacity:1 it just jumps straight to it.
Here is my code
Javascript:
AddEventListener(window, 'load', function () {
window.FadeinTime = 500;
var LargeImageDiv = document.getElementById('LargeImage');
window.LargeImage = LargeImageDiv.childNodes[1];
var LargeImageCaption = LargeImageDiv.childNodes[3];
var SlideText = ['Videography', 'Photography', 'Walkthroughs', 'Fine Art'];
window.Images = []; //Some image links
LargeImage.src = '/Resources/Home/' + window.Images[0];
window.AutoSlide = setInterval(function () {
var SlideNo = parseInt(LargeImage.getAttribute('data-slide')) + 1;
var SlideTextNo = parseInt(LargeImage.getAttribute('data-slideText')) + 1;
if (SlideNo == Images.length) SlideNo = 0;
if (SlideTextNo == SlideText.length) SlideTextNo = 0;
LargeImage.setAttribute('data-slide', SlideNo.toString());
LargeImage.setAttribute('data-slideText', SlideTextNo.toString());
LargeImageDiv.setAttribute('style', '-khtml-opacity:0;-moz-opacity:0;-ms-filter: alpha(opacity=0);-ms-filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);filter: alpha(opacity=0);opacity:0;');
setTimeout(function () {
LargeImage.src = '/Resources/Home/' + Images[SlideNo];
LargeImageCaption.innerHTML = SlideText[SlideTextNo];
LargeImageDiv.removeAttribute('style');
}, window.FadeinTime)
}, 2500)
});
HTML:
<div id="LargeImage" class='NoSelect' onclick='if(event.target.nodeName == "BUTTON") return; var u = this.childNodes[3].innerHTML; window.location=(u=="Videography"||u=="Walkthroughs"?"/Videos/View/"+u:"/Pictures/View/"+u);'>
<img src="" data-slide='0' data-slideText='0'/>
<pre class='Caption'>Videography</pre>
</div>
CSS:
body > div#Content > div#LargeImage {
cursor:pointer;
max-height:490px;
min-height:300px;
width:100%;
overflow:hidden;
margin:0 auto;
position:relative;
-moz-transition:.5s;
-webkit-transition:.5s;
-o-transition:.5s;
transition:.5s;
}
body > div#Content > div#LargeImage > button {
position:absolute;
bottom:5px;
right:5px;
;
}
body > div#Content > div#LargeImage > img {
width:100%;
margin-top:-10%;
}
body > div#Content > div#LargeImage > pre {
height:50px;
font-size:80px;
}