-2

When I call the below function Selectall, the alert doesn't appear.

 function Selectall(){
     $("INPUT[id^='DefaultContent_checkAllWebObjectCheckBox_").on('change',   function () {

    $("INPUT[id^='DefaultContent_checkLstWebObjects_']").prop('checked', $(this).prop("checked"));
    alert('Hello');
});
}
Monz
  • 139
  • 1
  • 13
  • 1
    Can you include a [mcve]? – Scimonster Apr 04 '16 at 15:01
  • 5
    Any errors in the console? maybe it doesn't complete the execution of the first line – Adjit Apr 04 '16 at 15:04
  • Possible duplicate of [How can I debug my JavaScript code?](http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code) – Liam Apr 04 '16 at 15:04
  • ok i minimized and verified, when the above function is called, alert doesnt get called. – Monz Apr 04 '16 at 15:05
  • `checked` is an attribute, use `.attr('checked')`, not `.prop()`. Also, use a `.each()` function to loop through the multiple inputs. – yaakov Apr 04 '16 at 15:06
  • the propblem isnt with my checked line of code, the alert hello isnt firing – Monz Apr 04 '16 at 15:09
  • @TricksfortheWeb `checked` is also a property, and `prop()` should always be used over `attr()` where possible. – Rory McCrossan Apr 04 '16 at 15:09
  • 1
    Your code as shown works fine: https://jsfiddle.net/hc6eepqs/. Please add an MCVE to your question. Note that if you're trying to set the inputs as selected you should set the checked property to `true`, not the value it already has. – Rory McCrossan Apr 04 '16 at 15:11
  • Are you actually running the function? Where's the rest of your code, because there doesn't seem to be anything wrong with this code, see @RoryMcCrossan's fiddle. – yaakov Apr 04 '16 at 15:11
  • Is the first line actualy running? – yaakov Apr 04 '16 at 15:13
  • yes the first line runs – Monz Apr 04 '16 at 15:13
  • You missed a apostrophe and bracket on the `.on('change')` function's selector:Not: `$("INPUT[id^='DefaultContent_checkAllWebObjectCheckBox_").` This: `$("INPUT[id^='DefaultContent_checkAllWebObjectCheckBox_']").` – yaakov Apr 04 '16 at 15:21

1 Answers1

0

is this function just for select all? or would you like to toggle all (by selecting/deselecting)?

function Selectall() {
    $("INPUT[id^='DefaultContent_checkLstWebObjects_']").attr('checked', true);
}

if you would like to toggle all:

<input type="checkbox" name="toogle_all" onclick="toggleAll(this)"/>

and your function would be like this:

function toggleAll(toggler) {
    $("INPUT[id^='DefaultContent_checkLstWebObjects_']").attr('checked', $(toggler).attr("checked"));
}
Diego Polido Santana
  • 1,425
  • 11
  • 19