Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Radio button default checked

Radio button default checked

Scheduled Pinned Locked Moved Web Development
question
8 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    astrixtech
    wrote on last edited by
    #1

    Hi , I am using the below code to convert the checkboxes in a list to radio buttons

    var element = checkbox.replaceWith('<input type="radio" id="rdList" style="width:12px; height:12px;" value="'+checkbox.attr('value')+'"/>');

    This changes all the check boxes to radio buttons. Since I needed on of the radio button to be checked ,I used the below code.

    document.getElementById("myradio").checked = true;

    This makes the first list item to be checked but only for few seconds. Then it unchecks by itself. I also used the below code for the same purpose.

    $("#myradio")[0].checked = true;

    The result is the same. Any idea why the check status doesnt stay as it is? Many thanks in advance.

    A T 2 Replies Last reply
    0
    • A astrixtech

      Hi , I am using the below code to convert the checkboxes in a list to radio buttons

      var element = checkbox.replaceWith('<input type="radio" id="rdList" style="width:12px; height:12px;" value="'+checkbox.attr('value')+'"/>');

      This changes all the check boxes to radio buttons. Since I needed on of the radio button to be checked ,I used the below code.

      document.getElementById("myradio").checked = true;

      This makes the first list item to be checked but only for few seconds. Then it unchecks by itself. I also used the below code for the same purpose.

      $("#myradio")[0].checked = true;

      The result is the same. Any idea why the check status doesnt stay as it is? Many thanks in advance.

      A Offline
      A Offline
      astrixtech
      wrote on last edited by
      #2

      If any one could help, then that will be greatly appreciated as I am stuck with this issue. I have googled a lot. But still I didnt get an answer why the selection disappears after few seconds. :(

      1 Reply Last reply
      0
      • A astrixtech

        Hi , I am using the below code to convert the checkboxes in a list to radio buttons

        var element = checkbox.replaceWith('<input type="radio" id="rdList" style="width:12px; height:12px;" value="'+checkbox.attr('value')+'"/>');

        This changes all the check boxes to radio buttons. Since I needed on of the radio button to be checked ,I used the below code.

        document.getElementById("myradio").checked = true;

        This makes the first list item to be checked but only for few seconds. Then it unchecks by itself. I also used the below code for the same purpose.

        $("#myradio")[0].checked = true;

        The result is the same. Any idea why the check status doesnt stay as it is? Many thanks in advance.

        T Offline
        T Offline
        thanh_bkhn
        wrote on last edited by
        #3

        What does "uncheck by itself" means? Do you have the refresh interval or element regenerate interval? Btw, your radio buttons have "rdList" in their id, but you used "myradio" to access them.

        A 1 Reply Last reply
        0
        • T thanh_bkhn

          What does "uncheck by itself" means? Do you have the refresh interval or element regenerate interval? Btw, your radio buttons have "rdList" in their id, but you used "myradio" to access them.

          A Offline
          A Offline
          astrixtech
          wrote on last edited by
          #4

          Uncheck means, deselected by its own. Suppose if any user checks a radio button, the whole list item will be highlighted showing its selected. But here, the list item is not highlighted ,only the radio button is checked.And that state is not stable. I dont have any refresh interval implemented. And the id is rdList. Its a typo. :doh: ,my mistake :-D

          T 1 Reply Last reply
          0
          • A astrixtech

            Uncheck means, deselected by its own. Suppose if any user checks a radio button, the whole list item will be highlighted showing its selected. But here, the list item is not highlighted ,only the radio button is checked.And that state is not stable. I dont have any refresh interval implemented. And the id is rdList. Its a typo. :doh: ,my mistake :-D

            T Offline
            T Offline
            thanh_bkhn
            wrote on last edited by
            #5

            I still have no idea about why your radio button got deselected by its own. But as you describe, if you want all your radio buttons act like a "group", you should set a name for all of them, like:

            <input type="radio" id="rdList" name="myRadio" value="1"/>
            <input type="radio" id="rdList" name="myRadio" value="2"/>
            <input type="radio" id="rdList" name="myRadio" value="3"/>

            A 1 Reply Last reply
            0
            • T thanh_bkhn

              I still have no idea about why your radio button got deselected by its own. But as you describe, if you want all your radio buttons act like a "group", you should set a name for all of them, like:

              <input type="radio" id="rdList" name="myRadio" value="1"/>
              <input type="radio" id="rdList" name="myRadio" value="2"/>
              <input type="radio" id="rdList" name="myRadio" value="3"/>

              A Offline
              A Offline
              astrixtech
              wrote on last edited by
              #6

              I have tried putting them in a group and making the first element checked. But the result is the same. Just now I have added runat="server" in radio button control. After that when I used document.getElementById("rdList").checked = true;, it threw error saying getelementID is having Null object. When I used $("#rdList")[0].checked = true;, it threw error saying '0' is null.

              T 1 Reply Last reply
              0
              • A astrixtech

                I have tried putting them in a group and making the first element checked. But the result is the same. Just now I have added runat="server" in radio button control. After that when I used document.getElementById("rdList").checked = true;, it threw error saying getelementID is having Null object. When I used $("#rdList")[0].checked = true;, it threw error saying '0' is null.

                T Offline
                T Offline
                thanh_bkhn
                wrote on last edited by
                #7

                You got error because of [runat="server"]. I can't understand why a radio got deselected by itself. Can you post your complete HTML code?

                A 1 Reply Last reply
                0
                • T thanh_bkhn

                  You got error because of [runat="server"]. I can't understand why a radio got deselected by itself. Can you post your complete HTML code?

                  A Offline
                  A Offline
                  astrixtech
                  wrote on last edited by
                  #8

                  Okay I thought the code started to work a bit cos of runat attribute. Thanks for the info. :) Please find below the code.

                  $(document).ready(function() {
                  var chbx = $(".s4-itm-cbx");
                  var element = chbx.replaceWith('<input type="radio" runat="server" name="rdList" style="width:12px; height:12px;" value="'+checkbox.attr('value')+'"/>');
                  //document.getElementById("#rdList").checked = true;
                  //$("#rdList").checked = true;
                  //$(this).trigger('click');
                  //$('input:radio[name=rdList]')['0'].checked = true;
                  $('input:radio[name=rdList]:nth(0)').attr('checked',true);
                  $("input:radio[name=rdList]:checked").trigger('click');

                  $("input[type='radio']").click(function(){
                  var crbx = document.activeElement;
                  $("input[type='radio']").each(function () {
                  if(this!= crbx && this.checked == true){
                  this.checked = false;
                  $(this).trigger('click');
                  }
                  });
                  });
                  });

                  Couple of other information: I am creating a Sharepoint list,where checkboxes are allowed. The requirement is ,the user should be able to select only one listitem at a time.Thats why I am changing the checkboxes to radio button. Again, the first list item should be selected by default.Hope, this information is sufficient.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups