I would like to remove my input value when disabled.
I found some solution here:
Reset disabled input field's value
but it didn't work in my case.
My code looks like this:
<figure class="question">
<label>
<div class="order">5</div>
<p class="question_title">Live stream<span class="asterisk">*</span></p>
</label>
<br>
<input name="live_stream" class="radiobtn" type="radio" value="Yes">Yes
<input name="live_stream" class="radiobtn" type="radio" value="No">No
<br>
</figure>
<script>
$("input[name=live_stream]").on('click', function() {
var autoRefresh = $('#autorefresh');
if ($(this).val() == "No") {
autoRefresh.show();
autoRefresh.find('input,select,radio').prop('disabled', false);
} else {
autoRefresh.find('input,select,radio').prop('disabled', true);
autoRefresh.find('input,select,radio').val()= null ;
}
});
</script>
<figure class="question" id="autorefresh">
<label>
<div class="order">6</div>
<p class="question_title">Autorefresh frequency (in seconds)<span class="asterisk">*</span></p>
</label>
<input type="number" id="autoref" name="autorefresh" min="10" max="900" message="Give value between 10 and 900 seconds">
</figure>
Is it achievable by JQuery or at least pure HTML?
You are so close. The problem is in your line autoRefresh.find('input,select,radio').val()= null ;. This is not valid jQuery. Instead, try replacing that line with this:
autoRefresh.find('input,select,radio').val('');
This clears the input value. Full working code:
$("input[name=live_stream]").on('click', function() {
var autoRefresh = $('#autorefresh');
if ($(this).val() == "No") {
autoRefresh.show();
autoRefresh.find('input,select,radio').prop('disabled', false);
} else {
autoRefresh.find('input,select,radio').prop('disabled', true);
autoRefresh.find('input,select,radio').val('');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<figure class="question">
<label>
<div class="order">5</div>
<p class="question_title">Live stream<span class="asterisk">*</span></p>
</label>
<br>
<input name="live_stream" class="radiobtn" type="radio" value="Yes">Yes
<input name="live_stream" class="radiobtn" type="radio" value="No">No
<br>
</figure>
<figure class="question" id="autorefresh">
<label>
<div class="order">6</div>
<p class="question_title">Autorefresh frequency (in seconds)<span class="asterisk">*</span></p>
</label>
<input type="number" id="autoref" name="autorefresh" min="10" max="900" message="Give value between 10 and 900 seconds">
</figure>
Related
I have a set of radio. If I select Value=1 the show custom_281 text field and if it is visible then should have some value before proceeding, if empty show an alert. If selected value=2 then hide custom_281 and not mandatory to fill in.
I cant seem to validate the text field and show the alert.
Here’s my html And jquery. Any help would be appreciated. Thanks
<div id="editrow-custom_280" class="crm-section editrow_custom_280-section form-item">
<div class="content">
<input type="radio" id="QFID_1_custom_280" name="custom_280" class="required crm-form-radio" value="1">
<input type="radio" id="QFID_2_custom_280" name="custom_280" class="required crm-form-radio" value="2">
</div>
</div>
<div id="editrow-custom_281" class="crm-section editrow_custom_281-section form-item" novalidate="novalidate" style="display: block;">
<div class="content">
<input id="custom_281" type="text" name="custom_281" maxlength="255" class="crm-form-text" placeholder="Enter Number ">
</div>
</div>
CRM.$(function($) {
showNumber();
function showNumber() {
$('input[name=custom_280]').change(showNumber);
if ($('input[name=custom_280][value=1]').is(':checked')) {
$('div#editrow-custom_281').slideDown('slow').validate({ignore:':not(:visible)'});
}
else {
$('div#editrow-custom_281').slideUp('slow');
}
}
});
You can check if the radio button checked value is 1 if yes check if the input is not empty depending on this show you message.
Demo Code :
showNumber();
function showNumber() {
$('input[name=custom_280]').change(showNumber);
if ($('input[name=custom_280][value=1]').is(':checked')) {
$('div#editrow-custom_281 input').val('')
$('div#editrow-custom_281').slideDown('slow')
} else {
$('div#editrow-custom_281').slideUp('slow');
}
}
//on click of proceed btn
$("#proceed").on("click", function() {
//check if the checked valeu is 1
if ($('input[name=custom_280]:checked').val() === '1') {
if (!$('div#editrow-custom_281 input').val()) {
$('div#editrow-custom_281 input').focus()
console.log("Please fill") //empty
} else {
console.log("All good")
}
}
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="crm-section editrow_custom_280-section form-item" id="editrow-custom_280">
<div class="content">
<input class=" required crm-form-radio" value="1" type="radio" id="QFID_1_custom_280" name="custom_280">
<input class=" required crm-form-radio" value="2" type="radio" id="QFID_2_custom_280" name="custom_280">
</div>
</div>
<div class="crm-section editrow_custom_281-section form-item" id="editrow-custom_281" novalidate="novalidate" style="display: block;">
<div class="content">
<input maxlength="255" name="custom_281" type="text" id="custom_281" class="crm-form-text" placeholder=" Enter Number ">
</div>
</div>
<button id="proceed">Proceed</button>
$('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);
});
I have two checkbox and under this when tick one of two checkbox I have two another option checkbox and another tick there will be text. The text will be value from the checkbox, how can I get the value from this checkbox? But the text I want to put are different sentence for each option checkbox selected not the word from checkbox. Did anyone know?
And my another problem is the checkbox not working if I do not tick the first checkbox.
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" /> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" /> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
Add value attribute to the checkbox. and set value to the checkbox.Or you can take the text from the label
<script type="text/javascript">
function ShowHideDiv(Cat) {
var dvCat = document.getElementById("bigCat");
bigCat.style.display = Cat.checked ? "block" : "none";
console.log(Cat.value)
console.log("Text Inside LABEL:" + Cat.parentNode.textContent )
}
</script>
<label for="Cat">
<input type="checkbox" id="Cat" onclick="ShowHideDiv(this)" value="cat"/> Cat
</label>
<script type="text/javascript">
function ShowHideDiv2(Rabbit) {
var bigRabbit = document.getElementById("bigRabbit");
bigRabbit.style.display = Rabbit.checked ? "block" : "none";
console.log(Rabbit.value)
console.log("Text Inside LABEL:" + Rabbit.parentNode.textContent )
}
</script>
<label for="Rabbit">
<input type="checkbox" id="Rabbit" onclick="ShowHideDiv2(this)" value="Rabbit"/> Rabbit
</label>
<div id="bigCat" style="display: none">
<label>
<input type="checkbox" id="bigSubCat" /> British shorthair
</label>
<label>
<input type="checkbox" id="bigSubCat" /> Exotic Shorthair
</label>
</div>
<div id="bigRabbit" style="display: none">
<label>
<input type="checkbox" id="bigSubRabbit" /> White Rabbit
</label>
<label>
<input type="checkbox" id="bigSubRabbit" /> Black Rabbit
</label>
I think you can follow this simple logic:
https://jsfiddle.net/pablodarde/6p7zkfwf/
HTML
<div class="container">
<div class="row">
<input type="checkbox" value="This is option one!" id="opt1"><label for="opt1">Option One</label>
<p></p>
</div>
<div class="row">
<input type="checkbox" value="This is option two!" id="opt2"><label for="opt2">Option Two</label>
<p></p>
</div>
</div>
JavaScript
const checkboxes = document.querySelectorAll('.container input[type=checkbox]');
for (let i = 0, l = checkboxes.length; i < l; i++) {
checkboxes[i].addEventListener('click', (e) => {
if(checkboxes[i].parentNode.querySelector('p').innerHTML == "") {
checkboxes[i].parentNode.querySelector('p').innerHTML = checkboxes[i].value;
} else {
checkboxes[i].parentNode.querySelector('p').innerHTML = "";
}
});
}
CSS
.row {
padding-bottom: 10px;
border-bottom: 1px solid black;
}
<script type="text/javascript">
function ShowHideDiv(chkCat) {
var dvCat = document.getElementById("dvCat");
dvCat.style.display = chkCat.checked ? "block" : "none";
}
</script>
<label for="chkCat">
<input type="checkbox" id="chkCat" onclick="ShowHideDiv(this)" /> Cat
</label>
<div id="dvCat" style="display: none">
<label>
<input type="checkbox" id="dvCat" onclick="ShowHideDiv(this)" /> British shorthair
</label>
<label>
<p id="British shorthair" ></p>
</label>
<script>
document.getElementById("dvCat").addEventListener("click", function(){
document.getElementById("British shorthair").innerHTML = "RM00";
});
</script>
<label>
<input type="checkbox" id="chkCat" onclick="ShowHideDiv(this)" /> Exotic Shorthair
</label>
<p id="Exotic Shorthair"></p>
<script>
document.getElementById("dvCat").addEventListener("click", function(){
document.getElementById("Exotic Shorthair").innerHTML = "RM00";
});
</script>
1 checkbox. Under this another 2 options checkbox. Select one of the two checkbox the output of both these options came out simultaneously. Try to run this coding. Can you fix my coding?
You can fix it for now by assigning unique ID to checkboxes.
<script type="text/javascript">
function ShowHideDiv(chkCat) {
var dvCat = document.getElementById("dvCat");
dvCat.style.display = chkCat.checked ? "block" : "none";
}
</script>
<label for="chkCat">
<input type="checkbox" id="chkCat" onclick="ShowHideDiv(this)" /> Cat
</label>
<div id="dvCat" style="display: none">
<label>
<input type="checkbox" id="dvSubCat" /> British shorthair
</label>
<label>
<p id="British shorthair" ></p>
</label>
<script>
document.getElementById("dvSubCat").addEventListener("click", function(){
document.getElementById("British shorthair").innerHTML = "RM00";
});
</script>
<label>
<input type="checkbox" id="chkSubCat" /> Exotic Shorthair
</label>
<p id="Exotic Shorthair"></p>
<script>
document.getElementById("chkSubCat").addEventListener("click", function(){
document.getElementById("Exotic Shorthair").innerHTML = "RM00";
});
</script>
P.S: Assigning id is not the ultimate solution, if you have more than 1 checkbox falling in same criteria use class names instead
for this jsfiddle - http://jsfiddle.net/Ja3Fx/1/ $('div').trigger('create') doesn't
work as required.
it does style radio button but it isn't quite right ... any ideas ?
<div id="testPage" data-role="page">
<div data-role="content">
<fieldset data-role="controlgroup">
<legend>Radio buttons, vertical controlgroup:</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked">
<label for="radio-choice-1">Cat</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2">
<label for="radio-choice-2">Dog</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3">
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4">
<label for="radio-choice-4">Lizard</label>
</fieldset>
<a id="add-animal" href="#" data-role="button" data-position-to="window">Add Animal</a>
</div>
</div>
$('#add-animal').click(function() {
var fldset = $('fieldset');
$(fldset).append($('<input type="radio" name="radio-choice-1" id="radio-choice-5" value="choice-5"><label for="radio-choice-5">Bat</label>'));
$("input").checkboxradio();
$('div ').trigger('create ');
});
I am using the following code to make a panel. You can try similar code.
$(document).on('pagecreate', '[data-role="page"]', function(){
$('<div>').attr({'id':'mypanel','data-role':'panel'}).appendTo($(this));
$(document).on('click', '#open-panel', function(){
$.mobile.activePage.find('#mypanel').panel("open");
});
});