0

HTML:

<input id="otherCheckbox2" type="checkbox" value="Accepted" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" /> 
  <span style="color: Black">Accepted</span> <br />
<input id="otherCheckbox3" type="checkbox" value="Contracted" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
  <span style="color: Black">Contracted</span> <br />
<input id="otherCheckbox4" type="checkbox" value="Pending" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
   <span style="color: Black">Pending</span><br />
<input id="otherCheckbox5" type="checkbox" value="Pre-Authorized" style="color: Black"  class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
   <span style="color: Black">Pre-Authorized</span> <br />
<input id="otherCheckbox6" type="checkbox" value="Show Deleted" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
   <span style="color: Black">Show Deleted</span> <br />
<input id="otherCheckbox7" type="checkbox" value="Treated" style="color: Black" class="chkdisplay" onchange="javascript:othercheckbxdisplay();" />
  <span style="color: Black">Treated</span> <br />  

MY javascript function with jquery but it does not work. called this function on a button click.Wen i click on button alert("hi"); is fired but not alert(index); Also explain my main question and just me the way for this question

 function ShowHideDxColumn() {
                alert("hi");
                $(".ckhdisplay").each(function (index) {
                    if ($(this).is(":checked")) {
                        alert(index);
                    }
                });
            }

thank u

0

2 Answers 2

3

You have a typo, .ckhdisplay should be .chkdisplay. Because of that, your .each call has no elements to iterate over because there are none with the given class.

 function ShowHideDxColumn() {
     alert("hi");
     $(".chkdisplay").each(function (index) {
         if ($(this).is(":checked")) {
             alert(index);
         }
     });
 }

You actually don't really need the condiiton in the each, you can just select the checked checkboxes:

$(".chkdisplay:checked").each(function(index){
    console.log(this);
});
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for notifying me my mistake i am really made?
will u explain me Can i use jquery methods,selectors in javascript function?
Yes of course you can use jQuery methods and selectors in Javascript functions, that's exactly where it's designed to be used.
0

Please try this:

$.each($(".chkdisplay"), function(index, element) {
    if ($(this).attr('checked')){
        alert(index);
    }
});

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.