Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I intend to write a piece of code to select one option in a radio button. I used the following code which is saw being used to select a dropdown. I modified it and used it but its not working. Could you please point out my error?
$("div.classradio radio").val("Male");
this is the code I used but it doesn't work.
$("div.classradio radio").prop("checked", true)
For versions of jQuery prior to 1.6, use:
$("div.classradio radio").attr('checked', 'checked');
Why you want do this with jQuery? It's very simple with HTML:
<form>
<input type="radio" name="gender" value="male" checked> Male<br>
<input type="radio" name="gender" value="female"> Female<br>
<input type="radio" name="gender" value="other"> Other
</form>
With jQuery you can do it like this:
$('input:radio[name=gender]')[1].checked = true;
The Number in the brackets indicates the radio button. Attention: jQuery starts counting at 0.
Please set the attribute of the input element by selector
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script>
$(document).ready(function () {
$('#FeMale').attr('checked',true);
});
</script>
</head>
<body>
<div class="container-fluid">
<input type="radio" name="Gender" value="Male" id="Male"/>Male
<input type="radio" name="Gender" value="FeMale" id="FeMale"/>Female
</div>
</body>
</html>
$("div.classradio radio").val("Male")
This code mean find the element with tag which is child of the div with class "classradio"
<div class="classradio">
<radio></radio>
</div>
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Improve this question
I want to hide all unchecked radio button and its label then display only the checked radio button using the submit button click event.
<form method="post">
<input type="radio" name="radiobtn">
<label for="first">First</label>
<input type="radio" name="radiobtn">
<label for="second">Second</label>
<input type="radio" name="radiobtn">
<label for="third">Third</label><br>
<input id="submit" type="submit">
</form>
I want to make this work using jQuery. Can anyone give me snippet of jQuery code on how to do this?
Do something simple like this
$('#submit').click(function(e) {
e.preventDefault(); // prevent the form submission
$('[name="radiobtn"]:not(:checked)') // get all unchecked radio
.hide() // hide them
.next() // get all label next to them
.hide(); // hide the labels
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form method="post">
<input type="radio" name="radiobtn">
<label for="first">First</label>
<input type="radio" name="radiobtn">
<label for="second">Second</label>
<input type="radio" name="radiobtn">
<label for="third">Third</label>
<br>
<input id="submit" type="submit">
</form>
Take this code for Example
<form>
<input type="radio" name="sex" value="male" checked>Male
<br>
<input type="radio" name="sex" value="female">Female
</form>
It allows a user to select one button at a time, I was always under the impression that javascript was the answer to this type of a solution. So my question is, is there any embedded javascript in the HTML radio button and if not, where is the function coming from?
Its the Browser's duty to do that for us.
Why?
Because the behavior is stated in the spec:
http://www.w3.org/TR/html/forms.html#radio-button-state-(type=radio)
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
My HTML script is
<html>
<body>
<b><font color="green">Please select a check box to change the color of text </font> </b><br>
<input type="checkbox" name="red" id="chkbx" > Red<br>
<input type="checkbox" name="green" id="chkbx" value="1"> Green <br>
<input type="checkbox" name="blue" id="chkbx" value="2"> Blue <br>
<input type="checkbox" name="orange" id="chkbx" value="3"> Orange <br>
<input type="checkbox" name="yellow" id="chkbx" value="4"> Yellow <br>
<p id="demo"></p>
<button onclick="myfunction()">My Choice</button>
<br><br>
<h style="color: red; font-weight: bold;"> </h>
<p id="demo"></p>
i need to get the checkbox value in javascript and if any one checkbox is selected that colour will apply to a text to display on the output. how to do?
First off - don't use the tag as it has been deprecated since 1999 use
<p style='font-weight:bold; color:green'>....</p>
Instead of checkboxs use radio buttons
<input type="radio" name="red" value="red" onClick="myFunction(this.value);"> Red<br>
Repeat the above for all possible selections.
Change your function myFunction() to myFunction( value ) and work from there. The information is passed directly to the function so there is no need to go looking for it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i have a text field , where i input the value to filter ....
<input type="text" name="filter" id="filter">
<input type="radio" name="item" id="item" value="1">apple
<input type="radio" name="item" id="item" value="2">banana
<input type="radio" name="item" id="item" value="3">grapes
<input type="radio" name="item" id="item" value="4">mango
<input type="radio" name="item" id="item" value="5">orange
<input type="radio" name="item" id="item" value="6">pineapple
if i input apple in text field, only radio button with apple has text value should appear , rest must hide . Is der any way to do that ?
Check out the jquery selectors page:
"Attribute Contains Selector [name*="value"]"
var name2lookFor = $('#filter').val();
var $matchingElements = $('input[type="radio"]').filter('[name*="'+name2lookFor +'"]');
There are a lot of other selectors, just find the one you need. You might need a combination to make a bit more custom search, but this should put you in the right track.
Offtopic: An id must be unique, you cant call them the same in the ID attribute.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
i need help to make one script
The script I'm trying to do is the following:
I have a page with multiple checkbox's corresponding products and they all have an associated value.
then I have a variable that is the value I have in the account, and what I wanted to do with that as I start choosing products by clicking on the checkbox, javascript would have to count the value that has already been chosen and see how and lacking and hide all products that have a higher value or have available
<?php
$totalmoney = '50';
?>
<div class="tags">
<label><input type="checkbox" id="arts" value="20" /> Arts </label>
<label><input type="checkbox" id="computers" value="40" /> Computers </label>
<label><input type="checkbox" id="phone" value="15"/> Phone </label>
<label><input type="checkbox" id="video-games" value="5" /> Video Games </label>
</div>
Help me please, i need this to my work :S
Start with calculating the values of selected checkboxes with JS something like this:
var $inputs = $('#arts,#computers,#phone,#video-games');
$inputs.on('click', function (event) {
$checked = $inputs.is(':checked');
totalChecked = 0;
$checked.each(function () {
totalChecked += $(this).val();
});
});
Now you can easily check the rest:
var toBeSpend = totalmoney - totalChecked;
$inputs.not(':checked').each(function () {
$(this).toggle($(this).val() > toBeSpend);
});