nilesh wani
nilesh wani

Reputation: 1291

get input text value when checkbox selected in li jquery?

I want to get only input value from li those checkbox is selected and send to the controller as key value pair means key as checkbox id and value as a input textbox value. There is multiple li inside ul.
I found something like a

$('li').find('input:checked, input[type=text]').map(function(i,el) {
        getInputVal=  el.type === 'checkbox' 
                ? el.value 
                :""; 
    });

But not working.

Following is the my html code structure.

<ul id="sortable">

<li id="row_39" class="img">
            <div class="checkbox"> 
                <input type="checkbox" value="Mzk=" class="set_left" name="remove_img[]" id="remove_img[]">
                <b><label class="set_center">1</label></b>
            </div> 
            <div class="img_main">
                <a rel="gallery" class="boxer" title="AA" href="/prod_images/prod_21_13905400722.jpg">
                    <img width="'200  height=" 136="" src="/prod_images/prod_21_13905400722.jpg">
                </a>
            </div>
            <div class="desc">
                <input type="text" value="AA" class="textbox" name="update_caption[]">
            </div>
    </li>
    <li id="row_43" class="img">
            <div class="checkbox"> 
                <input type="checkbox" value="NDM=" class="set_left" name="remove_img[]" id="remove_img[]">
                <b><label class="set_center">2</label></b>
            </div> 
            <div class="img_main">
                <a rel="gallery" class="boxer" title="AA" href="/prod_images/prod_21_13905400726.jpg">
                    <img width="'200  height=" 134="" src="/prod_images/prod_21_13905400726.jpg">
                </a>
            </div>
            <div class="desc">
                <input type="text" value="AA" class="textbox" name="update_caption[]">
            </div>
    </li>
          ..........    

Please suggest me some answer. Thanks in advance

Upvotes: 0

Views: 3822

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388416

I think what you are looking for is

var params = {};
$('li input:checked').each(function (i, el) {
    params[this.id] = $(this).closest('li').find('input:text').val()
});
console.log(params)

Note: Your checkbox id is static so you will be overriding the same key in the object, you li elements has a dynamic part in its id, do you want to use that as the key for the params object

Upvotes: 5

Related Questions