locked
JavaScript for a checkbox RRS feed

  • Question

  • User-1738841732 posted

    I have a dropDown with two items itemA and itemB
    and two checkBox below named chkBoxA and chkBoxB

    if the item in the dropdown selected is itemA
    either of the checkBoxes(chkBoxA and chkBoxAB) should be selected(like a radio button) but not both

    Can someone please help me with the jaascript.

    Thursday, October 10, 2019 1:23 PM

Answers

  • User-1738841732 posted

    i don't need a radio button, i want checkbox as per the req. All i need is either of the checkbox need to be checked, its should function simillar to a radio button, when itemB is selected from the dropdown.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 2:50 PM
  • User475983607 posted

    i don't need a radio button, i want checkbox as per the req. All i need is either of the checkbox need to be checked, its should function simillar to a radio button, when itemB is selected from the dropdown.

    I don't understand the requirement.  If the ItemB is selected option the select element then why check the ItemB checkbox too?  Why do you need redundant ItemB selections?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 4:15 PM
  • User409696431 posted

    If the checkbox is checked via Javascript, what would prevent the user from unchecking it?  This requirement is a bit confusing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 6:40 PM
  • User288213138 posted

    Hi mansooraabid,

    if the item in the dropdown selected is itemA
    either of the checkBoxes(chkBoxA and chkBoxAB) should be selected(like a radio button) but not both

    According to your description, I made demo for your. I get the select value by drop.options[drop.selectedIndex].value, then change the state of the checkbox according to the select value.

    The code:

     <script>
            function selectcb() {
                var drop = document.getElementById("ddlModels");
                var selevalue = drop.options[drop.selectedIndex].value;
                if (selevalue == "itemA") {
                    document.getElementById("chkBoxA").checked = true;
                    document.getElementById("chkBoxB").checked = false;
                }
                else {
                    document.getElementById("chkBoxB").checked = true;
                    document.getElementById("chkBoxA").checked = false;
                }
            }
        </script>
    
    
    
    <select id="ddlModels" onchange="selectcb()">
                    <option value="itemA">itemA</option>
                    <option value="itemB">itemB</option>
                
                </select><br /><br />
                chkBoxA:<input id="chkBoxA" type="checkbox" /><br />
                chkBoxB:<input id="chkBoxB" type="checkbox" />

    The result:

    Hope this can help you.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 11, 2019 7:49 AM

All replies

  • User475983607 posted

    I have a dropDown with two items itemA and itemB
    and two checkBox below named chkBoxA and chkBoxB

    if the item in the dropdown selected is itemA
    either of the checkBoxes(chkBoxA and chkBoxAB) should be selected(like a radio button) but not both

    Can someone please help me with the jaascript.

    Share the code you have written up to this point.

    Thursday, October 10, 2019 1:29 PM
  • User-1738841732 posted

    function enableOneView(checkName) {
    //alert("enableOneView");
    var groupStr = document.getElementById("s2id_MainContent_ddlGroup").title;
    if (groupStr !== null && groupStr !== undefined) {


    if (groupStr === "A" || groupStr === "B") {
    alert("mactes " + checkName);


    if (checkName === "details") {
    document.getElementById('<%=chkA.ClientID%>').checked = false;
    } else {

    document.getElementById('<%=chkB.ClientID%>').checked = false;
    }


    }
    }
    }

    Thursday, October 10, 2019 2:28 PM
  • User475983607 posted

    Your code does not clarify the requirements.  Can you explain how your code is supposed to work?  At this point is seems the design has redundant state.  Secondly, if you want a radio buttons then use radio buttons.

    Thursday, October 10, 2019 2:43 PM
  • User-1738841732 posted

    i don't need a radio button, i want checkbox as per the req. All i need is either of the checkbox need to be checked, its should function simillar to a radio button, when itemB is selected from the dropdown.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 2:50 PM
  • User475983607 posted

    i don't need a radio button, i want checkbox as per the req. All i need is either of the checkbox need to be checked, its should function simillar to a radio button, when itemB is selected from the dropdown.

    I don't understand the requirement.  If the ItemB is selected option the select element then why check the ItemB checkbox too?  Why do you need redundant ItemB selections?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 4:15 PM
  • User409696431 posted

    If the checkbox is checked via Javascript, what would prevent the user from unchecking it?  This requirement is a bit confusing.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 10, 2019 6:40 PM
  • User288213138 posted

    Hi mansooraabid,

    if the item in the dropdown selected is itemA
    either of the checkBoxes(chkBoxA and chkBoxAB) should be selected(like a radio button) but not both

    According to your description, I made demo for your. I get the select value by drop.options[drop.selectedIndex].value, then change the state of the checkbox according to the select value.

    The code:

     <script>
            function selectcb() {
                var drop = document.getElementById("ddlModels");
                var selevalue = drop.options[drop.selectedIndex].value;
                if (selevalue == "itemA") {
                    document.getElementById("chkBoxA").checked = true;
                    document.getElementById("chkBoxB").checked = false;
                }
                else {
                    document.getElementById("chkBoxB").checked = true;
                    document.getElementById("chkBoxA").checked = false;
                }
            }
        </script>
    
    
    
    <select id="ddlModels" onchange="selectcb()">
                    <option value="itemA">itemA</option>
                    <option value="itemB">itemB</option>
                
                </select><br /><br />
                chkBoxA:<input id="chkBoxA" type="checkbox" /><br />
                chkBoxB:<input id="chkBoxB" type="checkbox" />

    The result:

    Hope this can help you.

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 11, 2019 7:49 AM