0

I have a checkbox and a label side by side in an html. Depending on the number of output returned from server side, there can be any number of checkbox and label. For example, if 5 outputs are returned from the server, there will be 5 rows (each row is 1 checkbox and 1 label).

The text to be displayed on the label is a variable and generated at runtime depending on what the database returns. Using below code, I can show 5 rows of checkbox and label. How do I update the text at id="labelFollowup" at runtime so that instead of label it shows a new text runtime?

$(function(){
  let div = '<div class ="followupSteps">'+
  '<input type="checkbox" id="checkboxFollowup">'+
  '<label for="checkboxFollowup" id="labelFollowup">label</label>'+
  '</div>';
  for(var index=0; index<5; index++)
    {
      $('table').append('<tr> <td>'+div+' </td> </tr>')
     }
});

And on the html

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
</tr>
</table>
0

2 Answers 2

1

First of all you should make your ids for labels and inputs unique. Try this:

$(function(){
  let data = ['one','two','three','four','five'];
  for(var index=0; index<5; index++)
    {
    let div = '<div class ="followupSteps">'+
  '<input type="checkbox" id="checkboxFollowup'+index+'">'+
  '<label for="checkboxFollowup'+index+'" id="labelFollowup'+index+'">'+data[index]+'</label>'+
  '</div>';
    
    
      $('table').append('<tr> <td>'+div+' </td> </tr>')
     }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
</tr>
</table>

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

6 Comments

instead of hardcoding '1' here $('#labelFollowup1').text('hi');, how can I do that without hardcode, only depending on whatever the index value is?
@lilyhack it's just an example. I don't know where do you want to change these labels. Do you want to do it right in the loop?
yes, however it works, I have the data available when I am in the loop. I just dont want to show those hardcoded label, need to show the data that I get from database.
@lilyhack why don't you show an example of what you're talking? You're mentioning some server outputs without showing any example.
Don't append inside for loops. Use string concatenation instead and append only once outside.
|
0

Try this:

$(function(){
  const div = '<div class ="followupSteps">'+
  '<input type="checkbox" id="checkboxFollowup-{{labelId}}">'+
  '<label for="checkboxFollowup" id="labelFollowup">label {{labelId}}</label>'+
  '</div>';
  const tr = '<tr> <td>'+div+'</td> </tr>';
  const rows = new Array(5).fill(tr).map((tr, index) => tr.replace(/{{labelId}}/g, index+1)).join("");
  $('table').append(rows)
});

label 1 label 2 label 3 label 4 label 5

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.