0

Hi I have a grid which looks something like this:

  Serial No                  Document Name       Attachment
1(checkbox)                  abc               (img)
2(checkbox)                   xyz               (img)
3(checkbox)                   uio               (img)
4(checkbox)                   pop               (img)

When you click on the img on Attachement ,it opens a dialog box which has attached mutiple documents .AND there is a checkbox against each document.

Now what I want to do is if the user selects those checkboxes then it should add the attribute documentcode to a variable, similarly if the user checks another document, it appends that attribute to that varaiable and thus form a string. Also, if the user selects checkbox against serial number 1, the checkboxes against that relevant dialog box also gets checked. So in this case I want to append attributes too.

For this purpose, I added an onclick function on the checkbox of the dialog box

          if ($(el).is(':checked')) 
            {
                docCodes += DocumentCode + '♦';
            }

        }

Now when I am selecting checkboxes in dialog box it appends the document code , however when I uncheck it , it should remove that document code which is not happening. How do I do this? Also on parent checkbox, the attributes should get appended too

Unknown
  • 35
  • 6
  • 1
    So this *"dialog box"* is actually what? Is it only a `prompt()`? Something from a plugin? Something you already have working? – zer00ne Jun 11 '21 at 13:13
  • @zer00ne The user browses and uploads files against each document name, and the attached files are basically shown in attachment. – Unknown Jun 11 '21 at 13:21
  • Yeah, that's still too vague, specifics are needed in order to help you. What you are describing could be anything. We need to know *exactly how* these dialog boxes are made, where do they originate from, etc. – zer00ne Jun 11 '21 at 15:08
  • @zer00ne hi for html you can check this link. You can run the code snippet you will find what I am saying https://stackoverflow.com/questions/67931708/on-selecting-a-checkbox-tick-checkboxes-of-another-div/67932253?noredirect=1#comment120072228_67932253 – Unknown Jun 11 '21 at 16:28

1 Answers1

0

You can probably just do a string replace in else

var docCodes = '';
function addAttributes(docID, CdCode, el) {
  var str = docId + '♦';

  if ($(el).is(':checked')) {
    docCodes += str;
  } else {
    docCodes = docCodes.replace(str, '');
  }

  console.log(docCodes);
}
charlietfl
  • 170,828
  • 13
  • 121
  • 150
  • Your code is working for the part where I uncheck , the relevant doccode gets removed from the string. As mentioned in my question, I also want to do the same when selecting checkbox against serial number 1. When I select serial number 1 , the checkboxes in dialog boxes get auto ticked. So in this case I want those doc codes to append too. Since this function is called on the checkboxes of dialog box. So how can I achieve this? @charliefl – Unknown Jun 11 '21 at 13:26
  • It sounds to me like you might be better off using objects for this rather than manipulating a string. You can build a string from object/array easily when you need it. Some sample html as a [mre] would help understand this better – charlietfl Jun 11 '21 at 13:31
  • Hi can you check this link for html https://stackoverflow.com/questions/67931708/on-selecting-a-checkbox-tick-checkboxes-of-another-div/67932253?noredirect=1#comment120072228_67932253 @charlietfl – Unknown Jun 11 '21 at 16:28