0

I want to create list in JS and it must be populated by 'string' object by checking checkboxes near that particular string. Also, when the checkbox is unchecked, that particular string must be removed from list.

Kindly help me build this piece of code.

 <div class="row panel panel-default c-panel">
      <div class="panel-heading c-phead">LIST</div>
      <div class="panel-body">
        <table class="table table-striped" id="table">
          <thead>
            <tr>
              <th scope="col">#</th>
              <th scope="col">Name</th>
              <th scope="col">Customer</th>
              
              <th scope="col">Checked/Unchecked</th>
            </tr>
          </thead>
          
          
          <tbody>
            <c:forEach items="${values}" varStatus="status" var="allval">
                <tr>      
                    <td>${status.index + 1}</td>
                    <td>${getVlaue1()}</td>
                    <td>${getVlaue2()}</td>
                    <td>
                      <input type="checkbox" class="custom-control-input" id="check1" value="${getVlaue1()}">
                    </td>
                </tr>
            </c:forEach>
          </tbody>
          
          
        </table>
      </div>
      </div>
    </div>
  </div>
</div>

SCRIPT >>

$(document).ready(function(){   
        
        
        $('#check1').click(function(){
            
        });
        
        $('#table').DataTable();
                
});
4

2 Answers 2

1

You can use simple array:

const myArray = ['string1', 'string2', 'string3']


/* To add new element use .push() function: */

myArray.push('string4')


/* To remove specific element you can use: */

const index = myArray.indexOf('string3')
if (index > -1) {
  myArray.splice(index, 1)
}

console.dir(myArray)

Sign up to request clarification or add additional context in comments.

1 Comment

@MrCoconut...your answer worked for datatable that shows first 10 values...but when we go below 10 but as DataTable show 10 entries per page, from 11th onwards, it not taking any values. !!! Plz help
1

Can you share the code snippet?

List in JavaScript are called arrays. If you're learning JavaScript, I highly recommend W3Schools for beginners to get an overall idea of the language and how it works. But regarding arrays, this link describes what they are, how to create them, and shows some examples:

https://www.w3schools.com/js/js_arrays.asp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.