tags:

views:

76

answers:

1

Hello,

I want to get the id value of c_i. And each time c_i is clicked I want to increment the value at chk_count, using jquery

<div id='d<? echo $i; ?>' style='margin-bottom:8px; border:#cccccc thin solid; height:25px;'>
            <span style='color:#cccccc; margin-right:5px;'><? echo $i; ?></span>
            <span><? echo $row_dw_all['d1e']; ?></span>

            <span style='position:absolute;right:0px;'>
            <input type='checkbox' name='c_i<? echo $i; ?>' id="c_i<? echo $i; ?>" value='<? echo $row_dw_all['dle']; ?>'>
            </span>

            </div>

<input type="hidden" name="chk_count" id="chk_count" value="" />

Thanks Jean

+1  A: 

You mean something like this?

Live example: http://jsfiddle.net/hQkQL/

You didn't specifically state that you wanted to increment using the value stored in the ID, so this just does a simple increment of 1 each time.

 $('input:checkbox').click(function() {

    var theID = $(this).attr('id');  // Grab the ID of the checkbox

    var $hidden = $('#chk_count');

    if(!$hidden.val()) {        // Increment the hidden field
        $hidden.val(1);
    } else {
        $hidden.val( parseInt($hidden.val()) + 1 );
    }

    alert('theID: ' + theID + '\n\nthe count: ' + $hidden.val());
});​

If you wanted to increment using the number in the ID, you could do this:

http://jsfiddle.net/hQkQL/2/

(I don't know PHP, so it assumes the IDs are c_i1, c_i2, etc.)

$('input:checkbox').click(function() {

    var theID = $(this).attr('id').replace( 'c_i', '' );  // Grab the ID of the checkbox

    var $hidden = $('#chk_count');

    if(!$hidden.val()) {        // Increment the hidden field
        $hidden.val(theID);
    } else {
        $hidden.val( parseInt($hidden.val()) + parseInt( theID ) );
    }

    alert('theID: ' + theID + '\n\nthe count: ' + $hidden.val());
});​

Adjust the replace() if I'm wrong about the ID. If it is actually just a number, it is invalid since IDs must start with a letter [a-zA-Z].

patrick dw
I am accepting the answer, though not what I wanted, but helped me achieve to the right point :)
Jean
@Jean - I'm just curious, what did you want it to do? It was a little hard to tell. When you said *"I want to get the id value of c_i"*, did you mean that you wanted to value stored in the element with the ID c_i? Anyway, glad it helped.
patrick dw