0

Checkboxes in table1(#tbl1) in jsp created dynamically using ajax and jquery with id like : id="123" (firstId)

$.each(json, function(idx, obj) {

$("#tbl1").append('<input type="checkbox" id='+obj.firstId+' onclick=nextPopulate('+obj.firstId+'); >'); }

In table2(#tbl2), checkboxes is created on check of checkboxes in tbl1 and whose id will be :

id="123-001" (firstId-secondId)

$.each(json, function(idx, obj) {

$("#tbl2").append('<input type="checkbox" id="'+firstId+'-'+secondId+'" >'); }

In my javascript, i'm using an ajax call to remove checkboxes in tbl2 if checkbox in tbl1 is unchecked.

The id's of second set of checkboxes will be like : "123-001" ,"123-002", "123-003", "456-001"

So,to remove all checkboxes from tbl2 whose id starts with firstId (123), if checkbox with id 123 is unchecked in tbl1.

How can i do this ?

I can use .empty(). But how to specify id as 'starts with firstId' (123)

I'm trying:

if(!($('#'+firstId).is(':checked'))){

    $('#'+firstId'-').empty();
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Jobin
  • 1,281
  • 2
  • 11
  • 14
  • possible duplicate of [jquery ID starts with](http://stackoverflow.com/questions/5413841/jquery-id-starts-with) -- please use the search before you ask a new question. – Felix Kling Nov 27 '13 at 06:07
  • you can use class for the second checkbox list. Like class="123" for first 3 check boxes and class="456" for the last one.. – Anoop Joshi P Nov 27 '13 at 06:08
  • Duplicate of [Wildcards in jQuery selectors](http://stackoverflow.com/q/5376431/218196) – Felix Kling Nov 27 '13 at 06:09
  • and [Find html element which id starts with](http://stackoverflow.com/questions/2298730/find-html-element-which-id-starts-with) – Felix Kling Nov 27 '13 at 06:10
  • and [jQuery find all li with an id starting with total](http://stackoverflow.com/questions/1125324/jquery-find-all-li-with-an-id-starting-with-total) – Felix Kling Nov 27 '13 at 06:10
  • and [Function for all the ids that starting with “abc_google_” or “abc_facebook_”](http://stackoverflow.com/q/18041041/218196) – Felix Kling Nov 27 '13 at 06:11
  • and [How to get checkbox which starts with specific id](http://stackoverflow.com/q/15964029/218196) – Felix Kling Nov 27 '13 at 06:11
  • All found via http://stackoverflow.com/search?q=[jquery]+starts+with+id – Felix Kling Nov 27 '13 at 06:11
  • @FelixKling This question is not a duplicate as i cannot hardcode the id. So, i need the correct syntax to take the id dynamically. – Jobin Nov 27 '13 at 06:42
  • @beauflitz: You already know how to do that, exactly like in your code. You just have to combine it with the solution in the other answers. Shouldn't be so difficult. FWIW, the first question I linked to has exactly the solution you are looking for: http://stackoverflow.com/questions/5413841/jquery-id-starts-with. – Felix Kling Nov 27 '13 at 06:46

4 Answers4

0

It is simple. Use the attr Selector:

$('[id^=123]')
Shluch
  • 423
  • 4
  • 13
0

You can use Attribute Starts With Selector [name^="value"]

$('[id^=123-]:not(:checked)').empty();

Or, you can use remove

$('[id^=123-]:not(:checked)').remove();

You can also try call .not(':checked');

$('[id^=123-]').not(':checked').remove();

Description: Selects elements that have the specified attribute with a value beginning exactly with a given string.

Adil
  • 146,340
  • 25
  • 209
  • 204
  • I cannot hardcode as 123- . It should be firstId - . what is the syntax for this ? firstId will have the id. – Jobin Nov 27 '13 at 06:37
0

You can use ^

if(!($('[$id^="firstId"]').is(':checked'))){

    $(this).empty();
Somnath Kharat
  • 3,570
  • 2
  • 27
  • 51
  • I cannot hardcode as 123- . It should be firstId - . what is the syntax for this ? firstId will have the id. – Jobin Nov 27 '13 at 06:38
0
$("[id^=check"+webId+"]").fn();

Here, check is a string which we hardcode and webClassId is dynamically taken.

So,eg: id starts with check101 will be considered.

Jobin
  • 1,281
  • 2
  • 11
  • 14