creating quiz jquery, function load options store quiz - javascript

code: http://jsfiddle.net/HB8h9/7/
<div id="tab-2" class="tab-content">
<label for="tfq" title="Enter a true or false question">
Enter a Multiple Choice Question
</label> <br />
<textarea name="tfq" rows="3" cols="50"></textarea>
<p>Mark the correct answer</p>
<input type="radio" name="multians" value="A">A)</input>
<input name="Avalue" type="text">
<br>
<input type="radio" name="multians" value="B">B)</input>
<input name="Bvalue" type="text">
<br>
<input type="radio" name="multians" value="C">C)</input>
<input name="Cvalue" type="text">
<br>
<input type="radio" name="multians" value="D">D)</input>
<input name="Dvalue" type="text">
<br>
//different file below used as main page
$(document).ready(function() {
$("#second li ").click(function() {
$("#content").load("file_above.html .tabs");
});
});
trying to create a quiz using div elements from different file containing select option tags. Need to create a function which will load all the "appropriate div tags" according to the selected option and display the dropdown options again after each option has been selected. I'm not sure when to implement submit button either after each question type is loaded or somewhere between 5 - 10 questions. the submit button will "store" the questions selected into a quiz which will later be used for another user to answer.
I hope this makes sense, I'm not too familiar with jquery and any help would be highly appreciated.
Any other techniques which would be better suited are also welcomed.
thanks.

You must set an id to select element and hadle onChange event over this.
Somehting like:
$(document).ready(function(){
$('#selectionId').onChange(function(){
/*load new options into select element*/
$(this).append('option data-tab="tab-6">New option text</option>');
/*displaying desired div. Using HTML5 data attributes as in the fiddle*/
var selected=$(this).attr('data-tab');
if (selected==='tab-1'){
/*load or display tab-1 class div*/
}else...
});
});
You can have various html files with only the corresponding tab to load or in the same file you are defined the select element you can have the tab class divs with visibility attribute set to 'none' and show the targeted div after user selects an option.
Personally, I preferred the last one option.

I hope this fixes your problem
http://jsfiddle.net/r9sv8/
Use the .change() to listen to the options selected and display the div respect to it.
$('.tabs').change(function(){
$('.tabQues').hide('500'); // Hides all the divs
var changeVal = $(this).val(); //Takes the value of the <option>
if(changeVal=="selectOne"){
$('.'+changeVal).show('500'); //show the div you want
}else if(changeVal=="multiple"){
$('.'+changeVal).show('500');
}else if(changeVal=="trueFalse"){
$('.'+changeVal).show('500');
}else if(changeVal=="short"){
$('.'+changeVal).show('500');
}else if(changeVal=="program"){
$('.'+changeVal).show('500');
}else{
$('.tabQues').hide();
}

Related

If certain radio buttons checked, display div else display different div?

:) So I'm trying to create a user questionaire type form, it doesnt have to send to anyone just display a div once certain radio buttons are checked and if them certain radio buttons arent checked then display a different div, but also when the webpage opens/refreshes none of the div's I want to display to be displayed (if that makes sense?). I found some code on here to try but it didnt work for me, I'm a noob with JS & Jquery but was hoping anyone could shed some light on my problem. please find my code below.
Jquery?
if($('input[value=yes1]:checked,
input[value=yes2]:checked,
input[value=yes3]:checked,
input[value=yes4]:checked').length == 4){
$("#correct").show();
}else{
$("#correct").hide();
HTML
<div class="row">
<div class="left">
<div class="right answer">
<div class="leftradio">
<input type="radio" id="yes1" value="yes1" name="iCheck1">
<label>Yes</label>
</input>
</div>
<div class="rightradio">
<input type="radio" id="no1" name="iCheck1">
<label>No</label>
</input>
</div>
</div>
</div>
<div class="right">
<div class="right answer">
<div class="leftradio">
<input type="radio" id="yes2" value="yes2" name="iCheck2">
<label>Yes</label>
</input>
</div>
<div class="rightradio">
<input type="radio" id="no2" name="iCheck2">
<label>No</label>
</input>
</div>
</div>
</div>
Yes
No
Yes
No
CSS
#correct{width:100%; height:50px; background:green; display:none;}
#incorrect{width:100%; height:50px; background:red; display:none;}
I think this is what you're looking for. I had to change the number of "correct" answers to two because that's all the html you have.
JS:
$( "input" ).on( "click", function() {
if($('input[value=yes1]:checked, input[value=yes2]:checked').length === 2){
$("#correct").show();
}else{
$("#correct").hide();
}
});
Working JS Fiddle: http://jsfiddle.net/akj84vrq/12/
A little something like this would probably get you what you want.
http://jsfiddle.net/uu512erm/
Listen for change event on the radio inputs by $('input:radio').change(function(){}
and then just add the logic inside.
I'm not sure that this can be accomplished via CSS.
Now, you are checking against some values/elements that don't exist in your code (ie input[value=yes3]:checked, you don't have input with that value, check it for yourself).
I don't understand what exactly you want to accomplish, but I will still try to help and since you're new let's keep it very basic:
if ($('input[value=yes1]').is(':checked') && $('input[value=yes2]').is(':checked')) {
// this will fire only if input with values yes1 and yes2 is checked
$('div_to_show').show(); // put your own selectors here
$('div_to_hide').hide(); // put your own selectors here
}
if ($('input[value=no1]').is(':checked') && $('input[value=no2]').is(':checked')) {
// this will fire only if input with values no1 and no2 is checked
$('some_other_div_to_show').show(); // put your own selectors here
$('some_other_div_to_hide').hide(); // put your own selectors here
}
if ($('input[value=yes1]').is(':checked') && $('input[value=no1]').is(':checked')) {
# this will fire only if both input with values yes1 and no1 is checked
$('some_other_div_to_show').show(); # put your own selectors here
$('some_other_div_to_hide').hide(); # put your own selectors here
}
# and so on... just type in your conditions and that's it.
If you need some other cases, just add them using this pattern or construct one complex else if statement with all your scenarios.

Best approach to add and remove elements from an html form

I want to create a simple html form with different inputs element. The questions and the possible choices for each question are stored in a database. The idea is to render form and child input elements with PHP, so a simple PHP function will take care of typesetting according to whether is type="text" or type="radio":
<form>
First name: <input id="1" type="text" name="firstname">
Last name: <input id="2" type="text" name="lastname">
<input id="3" type="radio" name="sex" value="male">Male
<input id="4" type="radio" name="sex" value="female">Female
<div id="div_5_optional">
Maiden name: <input id="5" type="text" name="maidenname" disabled="disabled">
</div>
</form>
Now, by default input id="5" will be disabled. But I also want to remove it. I am able to get it with JavaScript by loading in my header (although I can't guarantee the move to be smart)
<script>
$(document).ready(function(){
$("[disabled=disabled]").parent().remove();
});
</script>
So far so good, the div element is removed. Yet, I want to put the element back and in the original position when the radio button corresponding to Female is clicked. I added in the header, just below the previous js script this
<script type='text/javascript' src='scripts/enable_elements.js'></script>
which loads this function
// enable_elements.js
$(document).ready(function(){
$('#4').click(function(){
$( '#5' ).prop( "disabled", false );
});
});
Yet the all thing doesn't work. I guess the problem could be the my ready(function)s are only loaded once at the beginning and then put to sleep? Should I structure my form differently?
You do not want to remove(), instead you want to hide().
$(document).ready(function(){
$("#5").hide();
});
Also you want to handle the click on #3:
$('#3').click(function(){
$( '#5' ).attr("disabled", "disabled").hide();
});
$('#4').click(function(){
$( '#5' ).attr("disabled", "").show();
});
Note: I kept the "disabled" attribute, but really you don't need it since it will be hidden when unselectable...
If you remove the #5, then it's lost and you cannot show it again. So clicking on Male, then Female, back on Male, and on Female again... would not work.
you need to create a eventhandlers for the female and male checkbox that call your function and displays or hides the field

Rails: two radio selects on same source list

Wondering how to approach this... Best to look at the picture to visualize the, hopeful, UI for a form for choosing options in a list. Users need to be able to make a first choice and a second choice for each option. One and only one can be selected in each column, and for that matter, each row.
At first I thought, naturally, 2 radio button groups might work...but not sure how? Perhaps hidden radio_buttons whose values are manipulated via Javascript/JQuery in a click event on each div? Event should also check/handle "collisions" when user tries to select same option for both choices.
Or, would this perhaps be better with two hidden collection_selects...or even simpler, just two hidden text_fields...which javascript can populate with the ID of the selected option?
Or maybe I'm overlooking something more obvious.
I'm new(ish) to javascripting with Rails so looking for advice/validation.
Thanks.
I think something like this is what your looking for:
HTML:
<form>
<p class="exclusiveSelection">
Selection One
<input type="radio" name="firstColumn"/>
<input type="radio" name="secondColumn"/>
</p>
<p class="exclusiveSelection">
Selection Two
<input type="radio" name="firstColumn"/>
<input type="radio" name="secondColumn"/>
</p>
<p class="exclusiveSelection">
Selection Three
<input type="radio" name="firstColumn"/>
<input type="radio" name="secondColumn"/>
</p>
<input type="button" id="submitForm" value="Submit">
</form>
JavaScript:
$(function() {
$(".exclusiveSelection input[type='radio']").click(function() {
$exclusiveSelection = $(this).parent();
$('input[type='radio']', $exclusiveSelection).attr('checked', false);
$(this).attr('checked', true);
});
});
It ensures that the values are unique across column and row and works with jQuery 1.2.6 - 1.7.1. There is also a JSFiddle example.
If you need help adapting this for Rails let me know, however it should be straight forward.

radio button show divs on the based on its value

I have a list of radio buttons on the basis of how many id in the mysql table.
when ever clock on the radio then it shows the div details
for example
radio buttons
<input name="value" type="radio" value="facebook" id="facebook"/> facebook
<input name="value" type="radio" value="google_plus" id="google_plus" /> Google plus
<input name="value" type="radio" value="orkut" id="orkut" /> orkut
divs
<div id="facebook" style="display:none;">
facebook is one of the most popular social networking website
</div>
<div id="google" style="display:none;">
google plus is the new social network
</div>
<div id="orkut" style="display:none;">
Orkut is a socila networking website powered by google
</div>
when select radio, I need to show divs on the base value="" value of the radio button.
the thing is that radio numbers and values will be on the base of mysql data
is there any option to show divs in Jquery ?
if u know help me pls
Hope, this piece of code would be of any help. here is my try to find a solution for your problem :)
$('input:radio').click(function(){
var idVal = $(this).val();
$('div').hide();
$('div[id='+idVal+']').show()
});
please find a working sample here: http://jsfiddle.net/u3AbT/3/
from what it seems like you're asking you want to know how to show a div via jquery?
.show() method
or .css() method example: $('nameOfYourDiv').css('display','block');
$('input[name="value"]').change(function(){
var id = $(this).val();
$('#' + id).show();
});

Display content based on Select Menu dropdown change

I'm currently using the following to display an input field when I change a dropdown menu selection:
<select id="delivery_country" name="d_country" onchange="
if (this.selectedIndex==14){
this.form['statesUSA'].style.visibility='visible'
}else {
this.form['statesUSA'].style.visibility='hidden'
};">
However, this only changes the input form element called "statesUSA".
If I want to show the div that this form element is inside, how do I do this?
For the record, my HTML reads:
<div id="usa">
<input type="text" id="statesUSA" />
</div>
Many thanks for any pointers.
use document.getElementById(id)
this:
<select id="delivery_country" name="d_country" onchange="if (this.selectedIndex==14){document.getElementById('usa').style.visibility='visible'}else {document.getElementById('usa').style.visibility='hidden'};">

Categories

Resources