How do I make a div animation slide up and disappear? - javascript

I know this may sound like a basic question but the truth is I'm not that of an expert with animations and it's hard to calculate the math from x and y. So, I would like to know how to make the "gl_banner" div simply slide up after a 0.5-1 second(s) and disappear and the content below it will be pushed up to its original position. What css properties do I use? css-animation? transition? How do I do it?
P.S. I did some research on animations and I saw a lot of animations, advanced animations but couldn't find a simple slide up animation. A little help is appreciated! Thanks!
HTML CODE:
<div id="gl_banner" style="display:none; visibility:hidden;">Good Luck! :)</div>
<form id="quiz" action="grade.php" method="post" style="visibility:hidden;">
<!--Question 1-->
<h3>1. How many percent of modern camera phones use CMOS?</h3>
<div>
<input type="radio" name="question-1-answers" id="question-1-answers-A" value="A" />
<label for="question-1-answers-A">A) 20%</label>
<br/>
<input type="radio" name="question-1-answers" id="question-1-answers-B" value="B" />
<label for="question-1-answers-B">B) 80%</label>
<br/>
<input type="radio" name="question-1-answers" id="question-1-answers-C" value="C" />
<label for="question-1-answers-C">C) 50%</label>
<br/>
<input type="radio" name="question-1-answers" id="question-1-answers-D" value="D" />
<label for="question-1-answers-D">D) 90%</label>
</div>
<!--Question 2-->
<h3>2. Which type of camera setting(s) is best for greater control and flexibility in terms of focusing on a subject?</h3>
<div>
<input type="radio" name="question-2-answers" id="question-2-answers-A" value="A" />
<label for="question-2-answers-A">A) Manual Focus</label>
<br/>
<input type="radio" name="question-2-answers" id="question-2-answers-B" value="B" />
<label for="question-2-answers-B">B) Auto Focus</label>
<br/>
<input type="radio" name="question-2-answers" id="question-2-answers-C" value="C" />
<label for="question-2-answers-C">C) Both A and B</label>
<br/>
<input type="radio" name="question-2-answers" id="question-2-answers-D" value="D" />
<label for="question-2-answers-D">D) Neither</label>
</div>
<!--Question 3-->
<h3>3. What are the three properties included in an exposure triangle?</h3>
<div>
<input type="radio" name="question-3-answers" id="question-3-answers-A" value="A" />
<label for="question-3-answers-A">A) White Balance, ISO, Low Light</label>
<br/>
<input type="radio" name="question-3-answers" id="question-3-answers-B" value="B" />
<label for="question-3-answers-B">B) Shutter Speed, Exposure, ISO</label>
<br/>
<input type="radio" name="question-3-answers" id="question-3-answers-C" value="C" />
<label for="question-3-answers-C">C) Aperture, ISO, Exposure</label>
<br/>
<input type="radio" name="question-3-answers" id="question-3-answers-D" value="D" />
<label for="question-3-answers-D">D) ISO, Aperture, Shutter Speed</label>
</div>
<!--Question 4-->
<h3>4. The higher the ISO, the more noise it produces in an image.</h3>
<div>
<input type="radio" name="question-4-answers" id="question-4-answers-A" value="A" />
<label for="question-4-answers-A">A) True</label>
<br/>
<input type="radio" name="question-4-answers" id="question-4-answers-B" value="B" />
<label for="question-4-answers-B">B) False</label>
</div>
<!--Question 5-->
<h3>5. What is the name of the smartphone you've seen all over this site?</h3>
<div>
<input type="radio" name="question-5-answers" id="question-5-answers-A" value="A" />
<label for="question-5-answers-A">A) Nokia Pureview 808</label>
<br/>
<input type="radio" name="question-5-answers" id="question-5-answers-B" value="B" />
<label for="question-5-answers-B">B) Nokia Lumia 1020</label>
<br/>
<input type="radio" name="question-5-answers" id="question-5-answers-C" value="C" />
<label for="question-5-answers-C">C) Nokia Lumia 925</label>
<br/>
<input type="radio" name="question-5-answers" id="question-5-answers-D" value="D" />
<label for="question-5-answers-D">D) Nokia Lumia 920</label>
</div>
<br />
<hr style="border-top:1px; border-style:solid; border-color: #000;" />
<input style="cursor:pointer;" type="submit" value="Submit Quiz" />
</form>
JavaScript Code:
function takeQuiz()
{
// hide the intro
document.getElementById('intro').style.display = 'none';
// display the quiz
document.getElementById('quiz').style.visibility = 'visible';
document.getElementById('gl_banner').style.display = 'block';
document.getElementById('gl_banner').style.visibility = 'visible';
}

Use jQuery library .
Ex:
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
$( "intro" ).hide(); //hide div "intro" immd
$( "intro" ).click(function( event ) {//hide div "intro" on click
event.preventDefault();
$( this ).hide();
});
$( "intro" ).show(); //to show after hide
$( "intro" ).fadeOut();
$( "intro" ).fadeIn();

Check an example made by me with click function from JQuery:
Knee's Fiddle
Have fun (:

Related

Clicked state; styling on groups of form controls

I have a group of radio controls in a form. Each one is wrapped in a <label> element for styling.
When the page loads, each label has a background color set on it via CSS.
I want a user to be able to click on any radio button from the group toggling it's parent <label> background color.
From a UX point of view, I need each checked control to have it's parent label background color toggled. I need this throughout the group of radio controls.
<form>
<label>
<input type="radio" name="question1">
</label>
<label>
<input type="radio" name="question1">
</label>
<label>
<input type="radio" name="question1">
</label>
<label>
<input type="radio" name="question2">
</label>
<label>
<input type="radio" name="question2">
</label>
<label>
<input type="radio" name="question2">
</label>
<label>
<input type="radio" name="question3">
</label>
<label>
<input type="radio" name="question3">
</label>
<label>
<input type="radio" name="question3">
</label>
My approach;
I'm aware one cant select a parent element using CSS so it's off to jQuery.
Using jQuery, I can apply some logic to the entire set of radio buttons like so:
$(':radio').each( function (i, el) {
$(this).on('change', function(){
if($(this).is(':checked')){
$(this).parent().css('background', '#ba9bc9');
}
$(this).on('blur', function(){
$(this).parent().css('background', '#09afed');
});
});
});
This works, however it only works for the current element selected. Clicking away loses focus. I need it to maintain state throughout the entire set of questions. So questions #1 - #3 could all end up with coloured backgrounds potentially.
You can find the radios with the same name, and remove the class from them, before adding the class to the radio that was clicked.
var $radios = $(':radio');
$radios.on('click', function(e){
$radios.filter('[name="'+ e.target.name +'"]').not(e.target).parent().removeClass('selected');
$(e.target).parent().addClass('selected');
});
.selected {
background-color: red; //or whatever
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
<input type="radio" name="question1">
</label>
<label>
<input type="radio" name="question1">
</label>
<label>
<input type="radio" name="question1">
</label>
<label>
<input type="radio" name="question2">
</label>
<label>
<input type="radio" name="question2">
</label>
<label>
<input type="radio" name="question2">
</label>
<label>
<input type="radio" name="question3">
</label>
<label>
<input type="radio" name="question3">
</label>
<label>
<input type="radio" name="question3">
</label>
If you are able and willing to refacter your code you can do this purely with css:
label {
background: #ccc;
}
[type="radio"]:checked+label {
background: #b00;
}
<form>
<input id="lbl01" type="radio" name="question1">
<label for="lbl01">01</label>
<input id="lbl02" type="radio" name="question1">
<label for="lbl02">02</label>
<input id="lbl03" type="radio" name="question1">
<label for="lbl03">03</label>
<br/>
<input id="lbl04" type="radio" name="question2">
<label for="lbl04">04</label>
<input id="lbl05" type="radio" name="question2">
<label for="lbl05">05</label>
<input id="lbl06" type="radio" name="question2">
<label for="lbl06">06</label>
<br/>
<input id="lbl07" type="radio" name="question3">
<label for="lbl07">07</label>
<input id="lbl08" type="radio" name="question3">
<label for="lbl08">08</label>
<input id="lbl09" type="radio" name="question3">
<label for="lbl09">09</label>
</form>

Proper JavaScript to show/hide inputs

I just want to know the proper function loop code for show/hide method.
This is the javascript for show/hide for the first (1) radio button:
function showHide(){
var chckbox = document.getElementById("chk");
var hiddeninputs = document.getElementsByClassName("hidden");
for(var i=0; i !=hiddeninputs.length; i++){
if(chckbox.checked){
hiddeninputs[i].style.display ="block";
}
else{
hiddeninputs[i].style.display ="none";
}
}
}
Yet I need the proper loop for having multiple objects (checkboxes) with seperated show/hide method. This is the first checkbox code:
<input type="checkbox" name="area" id="chk" onclick="showHide(this.checked);"/>
<label for="chk">Billing & Credit Management Systems</label>
<div class="hidden">
<input type="radio" name="area1" /> <label> Web Billin g </label> <br />
<input type="radio" name="area1" /> <label> CRIBS </label> <br />
<input type="radio" name="area1" /> <label> PPC </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label> <br />
</div>
And I need the loop code in order to prompt the show/hide to the following objects:
<input type="checkbox" name="area" id="chk1" onclick="showHide(this.checked);"/>
<label for="chk1">Customer Care Systems</label>
<div class="hidden">
<input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br />
<input type="radio" name="area1" /> <label> MVNO CRM </label> <br />
<input type="radio" name="area1" /> <label> Self-Care Site </label> <br />
<input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label> <br />
<input type="radio" name="area1" /> <label> IRS </label> <br />
<input type="radio" name="area1" /> <label> Online Guide </label> <br />
<input type="radio" name="area1" /> <label> TMOS </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label> <br />
</div>
Only the first checkbox prompts while the second one doesn't whenever I checked.
Not the exact answer you're looking for, but a bit simpler (less looping). Let me know if this works for you, I'll try my best to explain what's happening here.
I implemented this solution with the intention of forcing you to change as little as possible.
1) Assign a unique attribute to the checkbox and the div it belongs to. In this case I used "data-menu". In the onclick function, pass "this" instance of the element into the function showHide.
<input data-menu="1" type="checkbox" name="area" id="chk" onclick="showHide(this);"/>
<div class="hidden" data-menu="1">
2) Use the css class 'hidden' to hide your menu options.
.hidden {
display: none;
}
3) Re-work your JS function to dynamically add the hidden class when the box is checked. Since your menus are off by default, checking on naturally turns them on.
function showHide(e){
var menu = document.querySelector('div[data-menu="'+e.getAttribute('data-menu')+'"');
menu.classList.toggle('hidden');
}
Check out the below working snippet to see it in action.
function showHide(e){
var menu = document.querySelector('div[data-menu="'+e.getAttribute('data-menu')+'"');
menu.classList.toggle('hidden');
}
.hidden {
display: none;
}
<input data-menu="1" type="checkbox" name="area" id="chk" onclick="showHide(this);"/>
<label for="chk">Billing & Credit Management Systems</label>
<div class="hidden" data-menu="1">
<input type="radio" name="area1" /> <label> Web Billin g </label> <br />
<input type="radio" name="area1" /> <label> CRIBS </label> <br />
<input type="radio" name="area1" /> <label> PPC </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label> <br />
</div>
<input data-menu="2" type="checkbox" name="area" id="chk1" onclick="showHide(this);"/>
<label for="chk1">Customer Care Systems</label>
<div data-menu="2" class="hidden">
<input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br />
<input type="radio" name="area1" /> <label> MVNO CRM </label> <br />
<input type="radio" name="area1" /> <label> Self-Care Site </label> <br />
<input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label> <br />
<input type="radio" name="area1" /> <label> IRS </label> <br />
<input type="radio" name="area1" /> <label> Online Guide </label> <br />
<input type="radio" name="area1" /> <label> TMOS </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label> <br />
</div>
function showHide(element){
var chckbox = document.getElementById(element);
var hiddeninputs = document.getElementsByClassName(element);
for(var i=0; i !=hiddeninputs.length; i++){
if(chckbox.checked){
hiddeninputs[i].style.display ="block";
}
else{
hiddeninputs[i].style.display ="none";
}
}
}
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<input type="checkbox" name="area" id="chk" onclick="showHide(this.id);"/>
<label for="chk">Billing & Credit Management Systems</label>
<div class="chk">
<input type="radio" name="area1" /> <label> Web Billin g </label> <br />
<input type="radio" name="area1" /> <label> CRIBS </label> <br />
<input type="radio" name="area1" /> <label> PPC </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label> <br />
</div> <br>
<input type="checkbox" name="area" id="chk1" onclick="showHide(this.id);"/>
<label for="chk1">Customer Care Systems</label>
<div class="chk1">
<input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br />
<input type="radio" name="area1" /> <label> MVNO CRM </label> <br />
<input type="radio" name="area1" /> <label> Self-Care Site </label> <br />
<input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label> <br />
<input type="radio" name="area1" /> <label> IRS </label> <br />
<input type="radio" name="area1" /> <label> Online Guide </label> <br />
<input type="radio" name="area1" /> <label> TMOS </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label> <br />
</div>
</body>
</html>
try this this will work fine for you.
You're not using the parameter you're providing in the call of your function.
var chckbox = document.getElementById("chk");
will always find the first checkbox only to know if the other fields should be hidden or not and
var hiddeninputs = document.getElementsByClassName("hidden");
will allways hide all elements with class hidden. I think that
<input type="checkbox" name="area" id="chk" onclick="showHide(this);"/>
(i.e. change the parameter to the element rather than the .checked) in combination with
function showHide(elem){
var selector = "#" + elem.id + " ~ .hidden";
document.querySelector(selector).style.display = elem.checked ? 'block' : 'none';
}
will do more of what you want
call onchange function in input type checkbox
example
<input type="checkbox" name="area" id="chk" onchange="valueChanged(this.id)"/>Billing & Credit Management Systems
<input type="checkbox" name="area" id="chk1" onchange="valueChanged(this.id)"/> Customer Care Systems
<script type="text/javascript">
$(document).ready( function(){
$(".hidden").hide();
$(".hidden2").hide();
});
function valueChanged(id){
if(id== "chk"){
if($('#chk').is(":checked"))
$(".hidden").show();
else
$(".hidden").hide();
}
if(id== "chk1"){
if($('#chk1').is(":checked"))
$(".hidden2").show();
else
$(".hidden2").hide();
}
}
</script>
<div class="hidden">
<input type="radio" name="area1" /> <label> Web Billin g </label> <br />
<input type="radio" name="area1" /> <label> CRIBS </label> <br />
<input type="radio" name="area1" /> <label> PPC </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area1" placeholder="Please Specify"/></label><br /></label> <br />
</div>
<div class="hidden2">
<input type="radio" name="area1" /> <label> CRM (Customer Relationship) </label> <br />
<input type="radio" name="area1" /> <label> MVNO CRM </label> <br />
<input type="radio" name="area1" /> <label> Self-Care Site </label> <br />
<input type="radio" name="area1" /> <label> CMS (Trouble Ticketing) </label> <br />
<input type="radio" name="area1" /> <label> IRS </label> <br />
<input type="radio" name="area1" /> <label> Online Guide </label> <br />
<input type="radio" name="area1" /> <label> TMOS </label> <br />
<input type="radio" name="area1" /> <label> Others <input type="text" name="area2" placeholder="Please Specify"/></label><br /></label> <br />
</div>

Radio button clicked with label

I have multiple radio buttons. In the code that i have they are different pictures corresponding with different functions.
The user has to know which radio button is clicked. Therefor i'm trying to find a way to change the background of the radio button after it has been clicked.
In order for the code to work the <label> has to stay arround the <input> tag.
<div class="radio-toolbar">
<label class="BottomLeft">
<input id="tech" type="radio" ng-model="SelectedLayout" value="BottomLeft" />
</label>
<label class="BottomRight">
<input id="tech" type="radio" ng-model="SelectedLayout" value="BottomRight" />
</label>
<label class="Dual">
<input id="tech" type="radio" ng-model="SelectedLayout" value="Dual" />
</label>
<label class="DualRight">
<input id="tech" type="radio" ng-model="SelectedLayout" value="DualRight" />
</label>
<label class="Duplex">
<input id="tech" type="radio" ng-model="SelectedLayout" value="Duplex" />
</label>
<label class="Custom">
<input id="tech" type="radio" ng-model="SelectedLayout" value="Custom" />
</label>
</div>
Here's the JSfiddle
You can try this:
$('input[type=radio]')
.on('click', function() {
var $label = $(this).closest('label');
$('label').not($label).css('background-color', 'green');
$label.css('background-color', '#2C8BDE');
});
Here is the FIDDLE.
Also, you must have unique ID's in html.
To work your labels , your code should look like this.The problem is that , you cannot have same ID for all elements. ID is unique and if you want to work with label , you have to put for="#" attribute in your <label></label> element.So , remember <label></label> goes with for="" attribute , and that for="" attribute works with the id in the <input /> tag.And I've made your radio buttons non-multiple , so remove name="" attribute to work as multiple.
<div class="radio-toolbar">
<label for="first">
Tech<input id="first" type="radio" ng-model="SelectedLayout" name="radioButton" value="BottomLeft" />
</label>
<label for="second">
AnotherOne<input id="second" type="radio" ng-model="SelectedLayout" name="radioButton" value="BottomRight" />
</label>
<label for="third">
Third<input id="third" type="radio" ng-model="SelectedLayout" name="radioButton" value="Dual" />
</label>
<label for="fourth">
Fourth<input id="fourth" type="radio" ng-model="SelectedLayout" name="radioButton" value="DualRight" />
</label>
<label for="fifth">
Fifth<input id="fifth" type="radio" ng-model="SelectedLayout" name="radioButton" value="Duplex" />
</label>
<label for="sixth">
Sixth<input id="sixth" type="radio" ng-model="SelectedLayout" name="radioButton" value="Custom" />
</label>
</div>
Try this.I have changed backgorund color checked radio button to orange with jquery
$('input[type=radio]')
.on('click', function() {
var $label = $(this).closest('label');
$('label').not($label).css('background-color', 'green');
$label.css('background-color', '#2C8BDE');
});

how do I select a parameter a Javascript selector?

Hi there (I'm a complete JS newbie so please no bullying)
I'm trying to pass formID inside my score variable but I don't think I'm getting my selector right, can anyone tell me what I'm doing wrong ?
Edit: Sorry everyone, I'm getting tired. My issue: I cannot properly select the value of my score var.
JS
function addScore(formId) {
var show_id = $('#show_id-'+formId).val();
var user_id = $('#user_id-'+formId).val();
var score = $('input[value="tvshowrating' + formID +'"]').val();
HTML
<form id="40">
<div class="your-score">
<div class="">Your Score</div> <div id="flash"></div>
<input class="hover-star" type="radio" name="tvshowrating40" value="1" title="1" />
<input class="hover-star" type="radio" name="tvshowrating40" value="2" title="2" />
<input class="hover-star" type="radio" name="tvshowrating40" value="3" title="3" />
<input class="hover-star" type="radio" name="tvshowrating40" value="4" title="4" />
<input class="hover-star" type="radio" name="tvshowrating40" value="5" title="5" />
<input class="hover-star" type="radio" name="tvshowrating40" value="6" title="6" />
<input class="hover-star" type="radio" name="tvshowrating40" value="7" title="7" />
<input class="hover-star" type="radio" name="tvshowrating40" value="8" title="8" />
<input class="hover-star" type="radio" name="tvshowrating40" value="9" title="9" />
<input class="hover-star" type="radio" name="tvshowrating40" value="10" title="10" />
<input type="hidden" id="show_id-40" value="40" />
<input type="hidden" id="user_id-40" value="2" />
<span id="hover-test" style="margin:0 0 0 20px;"></span>
<input id="submitscore" type="submit" value="Submit scores!" onclick="addScore(40);" />
</div>
</form>
</div>
You probably want the checked value:
$('input[name="tvshowrating' + formID +'"]:checked').val()
Here is another question that answers your problem:
Get Value of Radio button group
or
How can I know which radio button is selected via jQuery?
You're selecting <input>s by value.
You probably want input[name=....

Find all radio groups which haven't been selected

I have a basic quiz/survey type application I'm working on, and I'd like to give the user a prompt before they submit if they haven't answered all the questions. All the questions are multiple choice using radio buttons:
<div class="question">
Q1: What is the second letter of the alphabet?
<div class="choices">
<input type="radio" name="question_1" value="1" /> A
<input type="radio" name="question_1" value="2" /> B
<input type="radio" name="question_1" value="3" /> C
</div>
</div>
<div class="question">
Q2: Which out of these is a berry?
<div class="choices">
<input type="radio" name="question_2" value="1" /> Apples
<input type="radio" name="question_2" value="2" /> Bananas
<input type="radio" name="question_2" value="3" /> Carrots
</div>
</div>
<div class="question"> ...etc
How do you find which groups haven't got an option ticked? Or at least, if there are any which haven't been answered?
jQuery is ok, and even preferred.
Ah, I figured it out:
$('div.question:not(:has(:radio:checked))')

Categories

Resources