Thursday, 22 June 2017

How do I check/uncheck a checkbox input or radio button?

// Check #x
$( "#x" ).prop( "checked", true );

// Uncheck #x
$( "#x" ).prop( "checked", false );

How to get the value of selected option in a select box using jQuery



<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>jQuery Get Selected Option Value hemant9807</title>
    <script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            $("select.country").change(function() {
                var selectedCountry = $(".country option:selected").val();
                alert("You have selected the country - " + selectedCountry);
            });
        });
    </script>
</head>

<body>
    <form>
        <label>Select Country:</label>
        <select class="country">
            <option value="china">china</option>
            <option value="india">India</option>
          
        </select>
    </form>
</body>

</html>

jQuery make checkbox checked on select's selected option



HTML

<div>
    <select name="id_dystrybutor_glowny" id="id_dystrybutor_glowny" value="">
        <option value="">select one</option>
        <option value="1">Dyst1</option>
        <option value="2">Dyst2</option>
        <option value="3">Dyst3</option>
    </select>
</div>
<input type="checkbox" name="id_dystrybutor[]" class="dystrybutor" value="1"> Dyst1
<br/>
<input class="dystrybutor" type="checkbox" name="id_dystrybutor[]" value="2"> Dyst2
<br/>
<input class="dystrybutor" type="checkbox" name="id_dystrybutor[]" value="3"> Dyst3
<br/>


JS

$(document).ready(function() {
    $('#id_dystrybutor_glowny').change(function() {
        var dID = $(this).find(":selected").val();
        $('.dystrybutor[value="' + dID + '"]').prop('checked', true)
            .siblings().prop('checked', false);
    });
});