0

I have a bunch of links on a page that I want to be clicked once a checkbox is checked off. Each of these links belong to the CSS class "batchClass".

How do I get these links to click once my checkbox is clicked? So far I have the basics of my JQuery function written, and it's going into the function but I can't get the links to fire.

function checkAll() {
    if ($('#checkbox').is(":checked")) {
          $('.batchClass').each(function () {
              $(this).trigger('click');
          });

    console.log("Inside the function.");
    }
}

Add this function is added to the checkbox in C# code..

CheckBox chkAllAccounts = new CheckBox();
chkAllAccounts.Attributes.Add("onclick", "javascript:checkAll()");

How these links are being built in the C#..

info[idx++] = "<a style=\"text-align:center;\" class=\"batchCheck\" id=\"verifiedLink_" + v.ID + "\" href=\"javascript:verifyStatement('" + v.ID + "', '0');\"><img id=\"verifiedState_" + v.ID + "\" src=\"/images/icons/box_checked.png\"></a>";

Could I just do a call to verifyStatement directly?

3 Answers 3

1

Use .change() event for listening change on checkbox:

$('#checkbox').change(function(){
if ($(this).is(":checked")) {
      $('.batchClass').each(function () {
          $(this).trigger('click');
      });
}});
Sign up to request clarification or add additional context in comments.

5 Comments

This works, in the sense that it does loop through all of the links that have that css class but the links are not being clicked.
are those anchor links?? for redirecting you will have to use: location.href = $(this).attr('href')
No they don't redirect. It changes an image on being clicked and then later on in the C# code the status of these links is used in some other functions.
i am sorry . i did not get it. can you put the html and click event code for them??
Check my edit. I added the stringbuilder for the html.
0

This probably is not an answer yet, but if you can address our confusion, I can update this.

In this FIDDLE I've:

Tried to give an example of the functionality that you describe. I'm not sure why you need anchors to change a picture, so I've given you buttons, but anything visible could be used.

Shown two pictures, that when you click on the respective buttons, the pictures change, the buttons outline become green, and and id of 'active' is given.

Put in code so that when the checkbox is clicked, it clicks all of the buttons on the page.

Let's just see if this is the functionality that you want (visible evidence of "clicked" -add green border- and hidden evidence of "clicked" - add an id), and then we can go from there.

JS

$('.mycheckbox').on('change', function(){
                $('.sometypeoflink1').trigger('click');
                $('.sometypeoflink2').trigger('click');
});

$('.sometypeoflink1').on('click', function(){
    $('.batchclass1 img').remove();
    $('.hidden img:eq(0)').clone().appendTo('.batchclass1');
    $('.sometypeoflink1').css( 'border', '3px solid green');
    $('.sometypeoflink1').attr('id', 'active1');
    console.log( $('.sometypeoflink1').attr('id') );
});

$('.sometypeoflink2').on('click', function(){
    $('.batchclass2 img').remove();
    $('.hidden img:eq(1)').clone().appendTo('.batchclass2');
    $('.sometypeoflink2').css( 'border', '3px solid green');
    $('.sometypeoflink2').attr('id', 'active2');
    console.log( $('.sometypeoflink2').attr('id') );
});

Comments

0

while not impossible to cause a 'click'. you're thinking about it wrong. Try calling your click events by function name

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.