Change the status of toggle using jquery [duplicate] - javascript

This question already has answers here:
Setting "checked" for a checkbox with jQuery
(43 answers)
Closed 3 years ago.
I am trying to change the status of toggle switch using jquery but not success.
this is the code of jquery
$('#bandwidthcap').checked='false';
and I tried using this way
$('#bandwidthcap').attr('checked','false')
this is the code of html
<input type="checkbox" name="bandwidthcap" id="bandwidthcap" class="switchery" data-color="primary"/>

You can use the .prop() function:
$('#bandwidthcap').prop("checked", true)
<input type="checkbox" name="bandwidthcap" id="bandwidthcap" class="switchery" data-color="primary" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Related

Which attribute will stop users changing input data but allow jQuery to modify? [duplicate]

This question already has answers here:
What's the difference between disabled="disabled" and readonly="readonly" for HTML form input fields?
(7 answers)
Closed 7 years ago.
I have seen people recommending
<input name="input" readonly="readonly">
and
<input name="input" disabled="disabled">
But which of these two will:
Stop users changing the value
Allow jQuery to update the value
Use readonly, because disabled will also prevent the field from being submitted.

Find out if ASP.NET radio button is checked without knowing the Id using jQuery [duplicate]

This question already has answers here:
Getting ID from asp.net runat server in jQuery
(8 answers)
Closed 7 years ago.
I have an ASP.NET Forms page and there is a fieldset and inside this there are items that each have a radio button. Basically I want to determine if a certain item is checked. However, it appears that it's location could move and therefore the id could change if the database is updated. How can I determine if the item is checked using if the id attribute can change? I don't think the value would change, how can I determine if it is checked by using the value attribute?
I would like to add a class as suggested but I really only want to use javascript and not change any of the ASP.NET code in the .aspx file.
<fieldset style="padding-left: 5px;">
<input id="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_8" type="radio" name="ctl00$ctl00$MainContent$ContentPlaceHolder$rblstRole" value="36">
<label for="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_8">Agency VP</label>
<br>
<input id="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_9" type="radio" name="ctl00$ctl00$MainContent$ContentPlaceHolder$rblstRole" value="13">
<label for="ctl00_ctl00_MainContent_ContentPlaceHolder_rblstRole_9">Agent</label>
...
You can use attribute selector of jQuery:
$('input[value="36"]').is(':checked')
and you can see the working demo here: http://jsfiddle.net/5637csyz/

replace standard checkbox with a custom image [duplicate]

This question already has answers here:
Pure CSS checkbox image replacement
(4 answers)
Closed 8 years ago.
I am using the dijit/form/CheckBox to successfully allow a user to toggle the visability of a Variable. I would like to replace the checkbox with a custom image, that would show a highlight if its checked. Do I need to recreate the wheel to get this done, or can I amend what I have thus far?
The checkbox is created with:
function updateAerial_1977Visibility(checkbox) {
// alert("working");
if (checkbox.checked) {
Aerial_1977.show();
} else {
Aerial_1977.hide();
}
}
and the checkbox is displayed in the HTML with:
<input id="Checkbox3" name="mycheck" type="checkbox"
data-dojo-type="dijit/form/CheckBox" value="agreed"
style="padding-right: 20px;" onclick="updateAerial_1977Visibility(this);" />
<label for="mycheck">77</label>
try adding a src="path/to/image" into the input

Can I save the value of a textbox? [duplicate]

This question already has answers here:
Get the value in an input text box
(13 answers)
Closed 8 years ago.
I have one text box and have written "ABC" in the text box, but I want to obtain the text using jQuery
<input type=text value="ABC">
But when I use jQuery's .html() it returns null
Here's a FIDDLE
<input type="text">
<span></span>
$('input').keyup(function() {
$('span').text($(this).val());
});
or
$('input').keyup(function() {
$('span').html($(this).val());
});

Change value of checkbox based on value of other form [duplicate]

This question already has answers here:
How to implement "select all" check box in HTML?
(31 answers)
Closed 9 years ago.
I'm trying to have a checkbox called 'All' that when checked, also checks the rest of the checkboxes in my form. I have basically no javascript experience so sorry if this is really basic. I patched this together from looking at posts like this and this.
<script type="text/javascript">
function checkIt(checkbox)
{
document.GetElementById("1").checked = true;
document.GetElementById("2").click();
}
</script>
My HTML looks like this:
<form>
<input type="checkbox" id="A" onclick="checkIt(this)">All<br></input>
<input type="checkbox" id="1">One<br></input>
<input type="checkbox" id="2">Two<br></input>
</form>
How can I get checkboxes 1 and 2 to change when I select checkbox All? Thanks.
Since you are not familiar with javascript, I suggest looking into the jQuery javascript library. Many coders find it simpler to learn/use, and there is no debate that it requires MUCH less typing.
Here are some introductory jQuery tutorials if you are curious.
To solve your problem, I added a class to the checkboxes that you wish to automatically check/uncheck, and used that class to check/uncheck the boxes.
Working jsFiddle here
HTML:
<form>
<input type="checkbox" id="A">All<br></input>
<input type="checkbox" class="cb" id="1">One<br></input>
<input type="checkbox" class="cb" id="2">Two<br></input>
</form>
JQUERY:
$('#A').click(function() {
// alert($(this).prop('checked'));
if ($(this).is(':checked') == true) {
$('.cb').prop('checked', true);
}else{
$('.cb').prop('checked', false);
}
});
Note that this solution uses jQuery, so you need the jQuery library loaded (usually put this line in your head tags):
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

Categories