Radio Button add url parameter - html

i am trying to add url parameter like ( www.example.com/product#green) this but i need multiple paramater like ( www.example.com/product#green#large ) after first parameter add size i wrote something but it doesn't work properly. But when user select size first should push parameter. When user click colour then size www.example.com/product#green#large
And i have another problem when i refresh the page the parameters stay there.I need to clear url parameters.I added jsFiddle link down below.
How can i do this with jquery.
var currentUrl = window.location.href;
$("#colour-stage label").click(function() {
var valColourInput = $('input[name=colour]:checked').val();
document.location.href = currentUrl + '#' + valColourInput;
});
$("#size-stage label").click(function() {
var valSizeInput = $('input[name=size]:checked').val();
document.location.href = currentUrl + '#' + valSizeInput;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="" method="get">
<fieldset id="colour-stage">
<legend>
Colours:
</legend>
<label>
<input type="radio" name="colour" value="Red">
<span>Red</span>
</label>
<label>
<input type="radio" name="colour" value="Blue">
<span>Blue</span>
</label>
<label>
<input type="radio" name="colour" value="Yellow">
<span>Yellow</span>
</label>
<label>
<input type="radio" name="colour" value="Green">
<span>Green</span>
</label>
</fieldset>
<fieldset id="size-stage">
<legend>
Size:
</legend>
<label>
<input type="radio" name="size" value="Small">
<span>Small</span>
</label>
<label>
<input type="radio" name="size" value="medium">
<span>Medium</span>
</label>
<label>
<input type="radio" name="size" value="large">
<span>Large</span>
</label>
</fieldset>
</form>
https://jsfiddle.net/tolga748/06ekvf72/

Related

Radio buttons deselect when I press anywhere else on page

I have created simple radio buttons so that the user can select the color they like. It worked for a while but has now stopped keeping the selected option. After selecting one, the option deselects itself when I press any other part of the page.
I never had the attribute 'name' of them the same before but they were working so I thought that might have been the issue that didn't solve it either.
<form name="newActivity" method="post" action="newActivity.php">
<?php include('errors.php'); ?>
<label class="colour-title">COLOUR</label>
<div class="select">
<label>
<input type="radio" name="colour" value="blue">
<span class="blue">BLUE</span>
</label>
<label>
<input type="radio" name="colour" value="purple">
<span class="purple">PURPLE</span>
</label>
<label>
<input type="radio" name="colour" value="pink">
<span class="pink">PINK</span>
</label>
<label>
<input type="radio" name="colour" value="green">
<span class="green">GREEN</span>
</label>
</div>
My radio buttons are also part of a form as shown above.
use id instead of class
The only difference between them is that “id” is unique in a page and can only apply to at most one element, while “class” selector can apply to multiple elements.
example
<form name="newActivity" method="post" action="newActivity.php">
<?php include('errors.php'); ?>
<label class="colour-title">COLOUR</label>
<div class="select">
<label>
<input type="radio" name="colour" value="blue">
<span id="btn-color">BLUE</span>
</label>
<label>
<input type="radio" name="colour" value="purple">
<span id="btn-color">PURPLE</span>
</label>
<label>
<input type="radio" name="colour" value="pink">
<span id="btn-color">PINK</span>
</label>
<label>
<input type="radio" name="colour" value="green">
<span id="btn-color">GREEN</span>
</label>
</div>

How to "lock" an html radiobutton after selection?

Given this checkbox html element:
<h4>This is a question?</h4>
<input type="radio" value="1" name="q">answer 1</input>
<input type="radio" value="2" name="q">answer 2</input>
<input type="radio" value="3" name="q">answer 3</input>
How can I lock the state of a checkbox after it has been selected? That is, how can I avoid the user to change his answer?
A hacky idea using CSS. You make a layer that will prevent any click event when one input is checked
.block {
position:relative;
}
.block input:checked ~ i {
position:absolute;
top:0;
left:0;
right:0;
bottom:0;
}
<div class="block">
<h4>This is a question? (blocked)</h4>
<input type="radio" value="1" name="q1">answer 1
<input type="radio" value="2" name="q1">answer 2
<input type="radio" value="3" name="q1">answer 3
<i></i>
</div>
<div >
<h4>This is a question? (not blocked)</h4>
<input type="radio" value="1" name="q2">answer 1
<input type="radio" value="2" name="q2">answer 2
<input type="radio" value="3" name="q2">answer 3
<i></i>
</div>
With Anush Bhatia's example, you would need to also use JQuery.
Here is a simple snippet without the use of JQuery:
function change(yourname) {
var radios = document.getElementsByName(youname);
for (var i=0, iLen=radios.length; i<iLen; i++) {
radios[i].disabled = true;
}
}
Remember that you should also call the function on change using
<input type="radio" value="1" onchange="change(\"q\");" name="q">answer 1</input>
$('input:radio').click(function(){
var $inputs = $('input:radio')
if($(this).is(':radio')){
$inputs.not(this).prop('disabled',true); // <-- disable all but checked one
}else{
$inputs.prop('disabled',false); // <--
}
})
Try using this.
Refer to the link below http://jsfiddle.net/ur9zxo2e/
You could make each question be in ONE element and apply a questionHandle function to each question element
var questionElement1=document.getElementById('q1')
function questionHandle(elem){
var answered=false
function listen(ev){
if(answered){return ev.preventDefault()}
//else, since this only happens IF not pressed already
answered=true //so that it will not be pressed again
//any other thing you want to do for when question is answered below after this INSIDE this function
console.log(`question ${ev.path[0].value} was chosen`)//an example of anything you can do
}
try{
[...elem.children]
.forEach(a=>a.addEventListener('click',listen))
}
catch(err){return Error(err)}
}
questionHandle(questionElement1)
<h4>This is a question?</h4>
<div id="q1">
<input type="radio" value="1" name="q">answer 1</input>
<input type="radio" value="2" name="q">answer 2</input>
<input type="radio" value="3" name="q">answer 3</input>
</div>
I will do that this way :
const myForm = document.forms['my-form']
myForm.radioChoice = {}
myForm.oninput = ({target}) =>
{
if( target.type === 'radio')
{
if (!myForm.radioChoice[target.name])
myForm.radioChoice[target.name] = target.value
else
myForm[target.name].value = myForm.radioChoice[target.name]
}
}
<form action="" name="my-form">
<fieldset>
<legend>Question 1</legend>
<label><input type="radio" value="1" name="q1" />answer 1</label>
<label><input type="radio" value="2" name="q1" />answer 2</label>
<label><input type="radio" value="3" name="q1" />answer 3</label>
</fieldset>
<fieldset>
<legend>Question 2</legend>
<label><input type="radio" value="1" name="q2" />answer 1</label>
<label><input type="radio" value="2" name="q2" />answer 2</label>
<label><input type="radio" value="3" name="q2" />answer 3</label>
</fieldset>
</form>

One label for multiuple inputs (radio)

$('label').click(function() {
id = this.id.split('-');
if (id[0] === '1') {
id[0] = '2';
} else {
id[0] = '1';
}
$('#' + id[0] + '-' + id[1]).prop('checked', true);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="one">
<input type="radio" id="1-1" name="1-level">
<label for="1-1" id="1-1">1</label>
<input type="radio" id="1-2" name="1-level">
<label for="1-2" id="1-2">2</label>
</div>
<div class="two">
<input type="radio" id="2-1" name="2-level">
<label for="2-1" id="2-1">1</label>
<input type="radio" id="2-2" name="2-level">
<label for="2-2" id="2-2">2</label>
</div>
Is it possible to have one label for multiple inputs (radio)? So if I press the label (e. q. for="1"), two input fields get checked (input with id 1 in div class one and two)? Here is my example:
<div class="one">
<input type="radio" id="1" name="level">
<label for="1">1</label>
<input type="radio" id="2" name="level">
<label for="2">2</label>
</div>
<div class="two">
<input type="radio" id="1" name="level">
<label for="1">1</label>
<input type="radio" id="2" name="level">
<label for="2">2</label>
</div>
Quote from ducumentation:
https://www.w3.org/TR/html401/interact/forms.html#h-17.9.1
The LABEL element may be used to attach information to controls. Each
LABEL element is associated with exactly one form control.
So only way to do what you asked is by using JS
HTML:
Use data-input-group-id instead of id and data-for-input-group instead of for. Also you'll now be able to use "1" and "2" not "1-1" etc.
JS:
$('[data-for-input-group]').click(function() {
let inputGroupId = this.dataset.inputGroupId;
$('[data-input-group-id = "'+inputGroupId+'"]').prop('checked', true);
});

Form Submission after dynamic addition of elements not framing the proper query-string

I have created a form with 2 input text fields in a jQuerymobile page. But On click of some button which is outside this form, I am dynamically adding 2 more input fields to this form inside the div "ratingInfo".
<form id="rate" method="GET" action="Ratings" data-ajax="false" onsubmit="return getRating();">
<label for="regid">Enter Registration ID : </label>
<input type="text" placeholder="regid" id="regid" name="regid" data-mini="true" data-ajax="false" style="width:240px;"/>
<fieldset style="margin-top:30px;" data-role="controlgroup" data-type="horizontal" >
<legend>Rate the session our of 5 :</legend>
<input type="radio" name="radio" id="rating1" value="1" checked="checked">
<label for="rating1">1</label>
<input type="radio" name="radio" id="rating2" value="2">
<label for="rating2">2</label>
<input type="radio" name="radio" id="rating3" value="3">
<label for="rating3">3</label>
<input type="radio" name="radio" id="rating4" value="4">
<label for="rating4">4</label>
<input type="radio" name="radio" id="rating5" value="5">
<label for="rating5">5</label>
</fieldset>
<div id="ratingInfo" style="margin-top:20px; margin-bottom:20px; color:rgb(177, 175, 175);"></div>
<button data-inline="true" data-rel="back" data-theme="c" type="reset" >Reset</button>
<button data-inline="true" data-transition="flow" type="submit" name="submit" id="submit" data-ajax="false" data-theme="b">Submit</button>
</form>
jQuery that I used in getRating() method to add dynamic elements is :
$( "#ratingInfo" ).empty();
$( "#ratingInfo" ).append( "<span >Session ID - </span><input type='text' style='width:240px; color:rgb(177, 175, 175);' name='eventId' id='eventId' disabled='true'>" );
$("#eventId").val(eventId);
$( "#ratingInfo" ).append( "<br/><span>Session Description - </span><input type='text' style='width:240px; color:rgb(177, 175, 175);' name='eventDesc' disabled='true' id='eventDesc'>" );
$("#eventDesc").val(eventDesc);
When I Submit the form, the query-string(in the URL) has only 2 parameters that is "regId" and "radio".
The remaining 2 input fields "eventId" and "eventDesc" are not added in the URL as well.
But onclick of reset button, all the fields including dynamically added fields are also reset.
What might be causing this problem?
As of now, I have resolved the issue.
Inside the "ratingInfo" div, I created input elements.
For them, I have given values through jquery.
I have added styles to "ratingInfo" div as:
display:none;
so that it is not visible.
Thanks.

change form action with radio buttons

I would like to implement something similar to a google search with radio buttons. Depending on the radio button selected, would change the type of search (search,images,video, etc).
Right now I have:
<div id ="searchtext">
<form method="get" id ="searchbox" action="http://www.google.com/search"/>
<input type="text" name="q" size="30" class="searchtext" maxlength="255" value="" />
<input type="image" value="Search" class="searchbutton" src="images/searchbar/searchbutton.png"/>
<br/>
</div>
<div id="radiosearch">
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://www.google.com/search?q=';" checked="checked"/> Web
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://images.google.com/images?q=';"/>Images
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://maps.google.com/maps?hl=en&tab=wl?q=';"/>Maps
<input type="radio" name="radiosearch" onclick="document.searchbox.action='http://www.youtube.com/results?q=';"/>Youtube
<span class = "class1">
Change Theme
</span>
</div>
However, clicking on the radio boxes is not changing the form action. Any Ideas?
Try
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://www.google.com/search?q=';" checked="checked"/> Web
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://images.google.com/images?q=';"/>Images
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://maps.google.com/maps?hl=en&tab=wl?q=';"/>Maps
<input type="radio" name="radiosearch" onclick="document.getElementById('searchbox').action='http://www.youtube.com/results?q=';"/>Youtube