Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

looping to generate unique id javascript

I want to get unique id in my javascript so, i make a looping and put it after the name of id. Here is my HTML code:

$(document).ready(function() {
  $('input[type="checkbox"]').on('click', function() {
    if ($(this).is(":checked")) {
      var a;
      for (a = 1; a < 300; a++)
        // if checked   add value
        $("#abc" + a).val($('#xyz' + a).attr('value'));
    } else {
      // if unchecked remove value
      $("#abc" + a).val("");
    };
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<input type="checkbox" name="check1" id="check1" />
<input type="text" id="xyz1" name="xyz1" value="3" />
<input type="text" id="abc1" name="abc1" />

<input type="checkbox" name="check2" id="check2" />
<input type="text" id="xyz2" name="xyz2" value="5" />
<input type="text" id="abc2" name="abc2" />

And here is my javascript code:

I add "a" after id name in order to generate the same id with text. But it still didn't working well.. Anyone can help me? I really Appreciates for any helping :-)

like image 291
Reza M Avatar asked Jun 07 '26 18:06

Reza M


1 Answers

Your code is correct, sir :-)

http://codepen.io/zer00ne/pen/dGKvBL/

You need to add this to the head:

<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

like image 122
zer00ne Avatar answered Jun 10 '26 07:06

zer00ne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!