0

I am Trying to Insert the Selected Value of the checbox into an Array and display the array value.. However I got this error valueSelected.push is not a function.. Can Anyone help me? Here's my code. Are there anything that I missed Out?

<html>
    <head>
        <script src="js/jquery-1.7.1.min.js"></script>
        <script src="js/jquery-ui.js"></script>
        <script src="js/jquery-ui.min.js"></script>
    </head>
    <body>
        <table id="dataTbl">
            <tr>
                <td><input type='checkbox' id='chkVerify' name='chkVerify' value='Item1'></td>
                <td>Item 1</td>
            </tr>
            <tr>
                <td><input type='checkbox' id='chkVerify' name='chkVerify' value='Item2'></td>
                <td>Item 2</td>
            </tr>
            <tr>
                <td><input type='checkbox' id='chkVerify' name='chkVerify' value='Item3'></td>
                <td>Item 3</td>
            </tr>
        </table>
        <button type="button" class="deletebutton" name="delete_video" title="Show Selected Value" onclick="DelRow();">Show Selected Value</button>

        <script>
            function DelRow(){
                var checkboxValArry=[];
                var $chkbox_checked    = $('tbody input[type="checkbox"]:checked', '#dataTbl');
                 if($chkbox_checked.length === 0){
                    alert("No Row Selected");
                 }

                 else{
                    var checkedBox = $('#chkVerify:checked');
                        for (var i=0; i<checkedBox.length; i++){
                            var valueSelected = $('#chkVerify:checked').val();
                            checkboxValArry = valueSelected.push();
                            alert(checkboxValArry);
                        }

                 }

            }
        </script>
    </body>
</html>

2 Answers 2

1

valueSelected is a string .

Push is only for array.

Moreover your are not pushing element correctly.

Try like this

checkboxValArry.push(valueSelected);
console.log(checkboxValArry);

EDIT

Your data retrive part is wrong

Try like this

var checkboxValArry=[];
$('#chkVerify:checked').each(function(){
   checkboxValArry.push($(this).val());
})
console.log(checkboxValArry);
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks.. It works.. But the value returns is incorrect.. If I click on Item 1 and Item 2, the value in the array should be Item1, Item2.. But It returns Item1, Item1
@NurWafiqa i have updated my answer .. just use .each instead of for loop
Okay. Thanks.. I will give a try
Yes.. It Works.. Thanks A lot
@NurWafiqa id must be unique .. you should use class for mutiple name instead of mutiple same id
|
1

You may simply use jQuery map method to do all the heavy lifting for you.

if($chkbox_checked.length === 0){
     alert("No Row Selected");
}
else
{       
      checkboxValArry = $chkbox_checked.map(function(){
                               return this.value;
                        }).get();;
      console.log(checkboxValArry);                  
}

Here is a working sample

Also, i noticed that you have same id value for more than one checckbox. That is invalid. You should keep unique id values.

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.