Reputation:
I intend to align the radio
buttons with the text side by side.
I've managed to align the radio
buttons vertically in a straight line, but I'm now unable to keep the radio
buttons and text side by side.
Code
var allQuestions = [{
question: "Who is the Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill"],
correctAnswer: 0
},
{
question: "Who is the Prime Minister of India?",
choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"],
correctAnswer: 2
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"],
correctAnswer: 1
},
{
question: " Who averaged one patent for every three weeks of his life?",
choices: ["Thomas Edison", "Tesla", "Einstein"],
correctAnswer: 0
},
{
question: "What continent is cut into two fairly equal halves by the Tropic of Capricorn?",
choices: ["Africa", "South America", "Australia"],
correctAnswer: 2
},
{
question: "What explorer introduced pigs to North America?",
choices: ["Christopher Columbus", "Galileo", "Mussorie"],
correctAnswer: 0
},
{
question: "What F-word is defined in physics as a “nuclear reaction in which nuclei combine to form more massive nuclei”? ",
choices: ["Furnace", "Fusion", "Fission"],
correctAnswer: 1
},
{
question: "What was the first planet to be discovered using the telescope, in 1781?",
choices: ["Uranus", "Jupiter", "Mars"],
correctAnswer: 0
},
{
question: "Who is the chief minister of West Bengal, India?",
choices: ["Buddhadev Bhattacharya", "Jyoti Basu", "Mamta Banerjee"],
correctAnswer: 2
},
{
question: "Which is the fastest growing religion in the world?",
choices: ["Islam", "Christianity", "Hinduism"],
correctAnswer: 0
}
/*
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
} */
];
$(document).ready(function() {
var currentquestion = 0;
var correctAnswers = 0;
$(function() {
$("#progressbar").progressbar({
value: 0
});
});
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
$("#next").click(function() {
if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
};
currentquestion++;
$(function() {
$("#progressbar").progressbar({
value: currentquestion * (allQuestions.length),
});
});
if (currentquestion < allQuestions.length) {
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
if (currentquestion == allQuestions.length - 1) {
$('#next').html("Submit");
//$('#next').off("click");
$('#next').click(function() {
// if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){
// correctAnswers++;
// };
// alert(correctAnswers);
alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers * 100 / (currentquestion)) + "%");
});
}
};
});
});
.ui-widget-header {
background-image: none !important;
background-color: #FF7860 !important; //Any colour can go here
}
label {
display: block;
text-align: center;
}
/*input[name="option"] {
float:left;
}*/
#progressbar {
margin: auto;
margin-top: 20px;
float: none;
width: 50%;
/*height: 100%;*/
}
#container {
text-align: center;
}
span {
margin: 5em;
padding: 3em;
}
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<h3 id="question"></h3>
<form id="form">
<input type="radio" name="option" value="0" id="option0" checked>
<label for='option0'>
<br/>
</label>
<input type="radio" name="option" value="1" id="option1">
<label for='option1'>
<br/>
</label>
<input type="radio" name="option" value="2" id="option2">
<label for='option2'>
<br/>
</label>
</form>
<br/>
<a href="#" id="next" class="button">Next</a>
<br/>
<div id="progressbar"></div>
</div>
Upvotes: 0
Views: 280
Reputation: 60543
change your display:block
in your label
to inline-block
and remove the <br />
outside the <label>
in HTML, wrap label/input
with a div
var allQuestions = [{
question: "Who is the Prime Minister of the United Kingdom?",
choices: ["David Cameron", "Gordon Brown", "Winston Churchill"],
correctAnswer: 0
},
{
question: "Who is the Prime Minister of India?",
choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"],
correctAnswer: 2
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"],
correctAnswer: 1
},
{
question: " Who averaged one patent for every three weeks of his life?",
choices: ["Thomas Edison", "Tesla", "Einstein"],
correctAnswer: 0
},
{
question: "What continent is cut into two fairly equal halves by the Tropic of Capricorn?",
choices: ["Africa", "South America", "Australia"],
correctAnswer: 2
},
{
question: "What explorer introduced pigs to North America?",
choices: ["Christopher Columbus", "Galileo", "Mussorie"],
correctAnswer: 0
},
{
question: "What F-word is defined in physics as a “nuclear reaction in which nuclei combine to form more massive nuclei”? ",
choices: ["Furnace", "Fusion", "Fission"],
correctAnswer: 1
},
{
question: "What was the first planet to be discovered using the telescope, in 1781?",
choices: ["Uranus", "Jupiter", "Mars"],
correctAnswer: 0
},
{
question: "Who is the chief minister of West Bengal, India?",
choices: ["Buddhadev Bhattacharya", "Jyoti Basu", "Mamta Banerjee"],
correctAnswer: 2
},
{
question: "Which is the fastest growing religion in the world?",
choices: ["Islam", "Christianity", "Hinduism"],
correctAnswer: 0
}
/*
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
},
{
question: "Who is the Prime Minister of America?",
choices: ["Donald Trump","Barack Obama","Hilary Clinton"],
correctAnswer:1
} */
];
$(document).ready(function() {
var currentquestion = 0;
var correctAnswers = 0;
$(function() {
$("#progressbar").progressbar({
value: 0
});
});
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
$("#next").click(function() {
if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) {
correctAnswers++;
};
currentquestion++;
$(function() {
$("#progressbar").progressbar({
value: currentquestion * (allQuestions.length),
});
});
if (currentquestion < allQuestions.length) {
$('#question').html(allQuestions[currentquestion].question);
$('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>");
$('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>");
$('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>");
if (currentquestion == allQuestions.length - 1) {
$('#next').html("Submit");
//$('#next').off("click");
$('#next').click(function() {
// if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){
// correctAnswers++;
// };
// alert(correctAnswers);
alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers * 100 / (currentquestion)) + "%");
});
}
};
});
});
.ui-widget-header {
background-image: none !important;
background-color: #FF7860 !important;
}
h3, #next {
text-align: center;
display:block
}
#form div {
margin: auto;
max-width: 205px
}
label {
display: inline-block;
}
input[name^="option"] {
float: left;
}
#progressbar {
margin: auto;
margin-top: 20px;
float: none;
width: 50%;
/*height: 100%;*/
}
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
<h3 id="question"></h3>
<form id="form">
<div>
<input type="radio" name="option" value="0" id="option0" checked>
<label for='option0'></label>
</div>
<div>
<input type="radio" name="option" value="1" id="option1">
<label for='option1'></label>
</div>
<div>
<input type="radio" name="option" value="2" id="option2">
<label for='option2'></label>
</div>
</form>
<br/>
<a href="#" id="next" class="button">Next</a>
<br/>
<div id="progressbar"></div>
</div>
Upvotes: 1
Reputation: 4579
See my edited answer and the fiddle :
.rad_in {
position: absolute;
left : 25%;
}
https://jsfiddle.net/8fv3cnc6/
PS: Sorry, i had to post another answer since I can't comment...
Upvotes: 0
Reputation: 4579
Just set your CSS for label to display "inline-block":
CSS
label{
display: inline-block;
text-align: center;
}
HTML
<form id="form">
<label for='option0'></label><input type="radio" name="option" value="0" id="option0" checked><br />
<label for='option1'></label><input type="radio" name="option" value="1" id="option1"><br />
<label for='option2'></label><input type="radio" name="option" value="2" id="option2"><br />
EDIT
So, both answers don't solve the problem...
What you have to do is that :
1) Set you CSS for label :
label{ display: inline-block; }
2) Create a new class for radio buttons :
.rad_in {
position: absolute;
left : 25%;
}
3) And then your html goes like this (with the radio button class) :
<form id="form"> <input class="rad_in" type="radio" name="option" value="0" id="option0" checked><label for='option0'></label><br /> <input class="rad_in" type="radio" name="option" value="1" id="option1"><label for='option1'></label><br /> <input class="rad_in" type="radio" name="option" value="2" id="option2"><label for='option2'></label><br /> </form>
Here is the fiddle : https://jsfiddle.net/8fv3cnc6/
Upvotes: 0