3

I have a working loop that finds all the input tags in a Html form. My problem is that it wont save what state the radio button are in to the custom list in SharePoint online. I have other loops for option and choice input that gets saved like it should, and the same goes for a loop of text area.

All of the input tags are type="radio" except for one that are type="text" and a date "and that one gets saved".

In the Custom list in SharePoint online. The radio button should be saved in a column with Yes/No checkbox and the default value for that are no if not checked to be yes.

*** in the App.js file

$(document).ready(function () {
  $("#tabs").tabs();
  $(document).tooltip();
  $("#btnSave").click(createListItem);
  $("#GEN_4").datepicker({ dateFormat: "yy-mm-dd" }).val();
});


function createListItem() {
 listCreationInformation = new SP.ListItemCreationInformation(); 
 listItem = list.addItem(listCreationInformation);

 var input_list = $('input');

 var id, name, text;

 for (var i = 0; i < input_list.length; i++) {
     name = $(input_list[i]).attr('name');

     if ($(input_list[i]).attr('type') === 'radio') {
         //text = $(input_list[i]); //this code should be added to in some way!

          // I have tried these versions, and none of them will work.

         // text =$(input_list[i]).is(':checked');
        // text = $(input_list[i]).prop('checked');
         //text = $(input_list[i].checked('type') & 'radio');

     }
     else {
         text = $(input_list[i]).val();
     }
     listItem.set_item(name, text);
  }
  listItem.update(); //Update the List Item
  clientContext.executeQueryAsync(
    Function.createDelegate(this, success),
    Function.createDelegate(this, fail)
   );
 }

***in the aspx page

   <td>GEN_19_BYGG_GOLV</td>
   <td id="GEN_19">
       <input name="GEN_19" type="radio" value="1">Ja
        <br/>
        <input name="GEN_19" type="radio" value="0">Nej
    </td>
1

I am not sure why you need to loop. You can get the input element using jQuery Selector.

In case of radio button use following to identify if its selected or not

if($('#radio_1').is(':checked')) {
    //Save Yes
}
else {
    //Save No
}

Update the HTML as follows

<td>GEN_19_BYGG_GOLV</td>
<td id="GEN_19">
   <input id="radio_1" name="GEN_19" type="radio" value="1">Ja
    <br/>
    <input id="radio_2" name="GEN_19" type="radio" value="0">Nej
</td>

Updated based on comment

$('input[name=GEN_19]:checked').val()

The above will give you the value of selected radio button who is in the group "GEN_19"

  • It's not the solution I'm looking for. if it was only one radio button there would be no need for the loop, but now it's 14 radio buttons in a larger form. The grouping by name ensures that only one can be selected simultaneously by those who have the same name. My problem is that the value from the radio button I choose are not saved from the form to the custom list in SharePoint. Please help! – CodeCat Oct 23 '14 at 13:36
  • I have updated my answer – Amal Hashim Oct 23 '14 at 13:56
1

This is the solution that worked! Using .prop('checked') and setting the listItem in both if and else.

 for (var i = 0; i < input_list.length; i++) {
    name = $(input_list[i]).attr('name');

    if ($(input_list[i]).attr('type') === 'radio') {
        if ($(input_list[i]).prop('checked')) {

            text = $(input_list[i]).val();
            listItem.set_item(name, text);
        }
    }
    else {
        text = $(input_list[i]).val();
        listItem.set_item(name, text);
    }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.