1

I have a tree with checkboxes and their id has key for item and value as their value.

<input type="checkbox" name="list_code[4]" id="list_code[4]" value="AG12345678" checked="checked">

When users select a tree element I can access those by

$('input[name^="list_code"]').each(function() {
    if ($(this).attr('checked')) 
        list_code = $(this).val();
});

I am able to get value AG12345678 but I need to get key value too which is 4 from list_code[4] in this case. How can I access that value?

4
  • The answers so far seem like overkill. Just out of curiosity, how are you rendering your checkboxes? Commented Apr 17, 2012 at 18:53
  • [ and ] aren't valid characters for the id attribute. stackoverflow.com/questions/70579/… Commented Apr 17, 2012 at 18:55
  • @iambriansreed: Unless you're in an environment that only needs to target HTML5 browsers. Then they're fine. Commented Apr 17, 2012 at 18:58
  • @Robin I am using dynatree inside a dropdown box. When expanded users can select from tree or write to box which auto completes and selects auto completed element. Commented Apr 17, 2012 at 18:59

3 Answers 3

3
var n = this.id.slice(this.id.indexOf('[') + 1, this.id.indexOf(']'));

or...

var n = this.id.replace(/\D/g, '');

or...

var n = (this.id.match(/\d+/) || [])[0];

or if there could be other unwanted numbers...

var n = (this.id.match(/\[(\d+)\]/) || [])[1];

Or if you control the source, a good solution would be to use a data- attribute for future HTML5 support...

<input type="checkbox" data-number="4" name="list_code[4]" id="list_code[4]" value="AG12345678" checked="checked">

...then in HTML5 browsers, you could do...

this.data.number;

...or for legacy support, you could do...

this.getAttribute('data-number');
Sign up to request clarification or add additional context in comments.

5 Comments

+1 although I don't usually vote on answers that I am competing with
@ajax333221: Is it a competition? ;) I upvoted the other answers to even things out.
I used this.id.replace(/\D/g, '') and works fine. Just curious is .split has more overhead than .replace ?
@PoX: Generally I'd assume it does since it needs to create an Array, but the difference will be minor overall.
@PoX but that will replace every non-numeric chars, so list2_code[4] will be 24, just be careful (or use .split(/\[|\]/)[1])
2

With this:

this.getAttribute("id").split(/\[|\]/)[1];

Explanation:

  • this.getAttribute("id") gets the id "list_code[4]"
  • split(/\[|\]/) splits it into ["list_code","4",""]
  • [1] takes the element at index 1 which is 4

Comments

1

Try:

$('input[name^="list_code"]').each(function() {
    if ($(this).is(':checked')) 
        list_code = $(this).val();
        key = parseInt($(this).attr('name').replace(/[^0-9]/g,''));
});

If you find your field name attributes have numbers outside the index then do the following:

        key = parseInt($(this).attr('name').split('[')[1].replace(/[^0-9]/g,''));

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.