question

Jonathan-3830 avatar image
0 Votes"
Jonathan-3830 asked Jonathan-3830 edited

There's a way to catch any word that comes after a given string?

Hello, I am doing a checkbox list and in this list there is a check that checkbox that check all other checkboxes, I did a function on jquery that can do this task and works fine, however when I put "runat=server" in all my checkboxes it name changes and it doesn't work anymore.

The names of my checkboxes :

"ctl00$ContentPlaceHolder1$chkEmpresa
ctl00$ContentPlaceHolder1$chkEmpresaCod
ctl00$ContentPlaceHolder1$chkEmpresaCodRefExterno
ctl00$ContentPlaceHolder1$chkEmpresaRazaoSocial
ctl00$ContentPlaceHolder1$chkEmpresaCNPJ"

I wanted to catch the name 'ctl00$ContentPlaceHolder1$chkEmpresa' and whatever comes after, it takes, no matter what


And here is my jquery function :

"function CheckAll(checkbox) {

         $("#chkEmpresa").click(function () {
             $('input[name="' + checkbox.name + '"]').not(checkbox).prop('checked', checkbox.checked);
         })

           
     }"


If anyone want to see the front-end code of the checkboxes, just tell me and i send a print or another type of document. I don't know why, but i can't attach my txt here


dotnet-aspnet-webformsdotnet-aspnet-webpages
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

cooldadtx avatar image
0 Votes"
cooldadtx answered Jonathan-3830 edited

It does that because names need to be unique for the server side to find it. In this case your checkboxes are contained in a container and the container's name is prepended to each control's name. The correct solution for your existing approach, to me, is to use jQuery to get the parent control in jQuery. How you do that really depends on where this script is being called from. Ideally it should be a relative reference.

Given the parent container you can get all the child checkboxes using a selector. Perhaps something like this.

var checkboxes = $('input[type=checkbox]');


For a probably better solution look at the answer here.

An even better solution might be to put a class on the checkboxes and just grab those using jQuery if you only need this functionality in one place.

· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hummmm, thanks @cooldadtx, I'll se it here

0 Votes 0 ·

Now it works, I had to put a class on the checkboxes and grabbed it. Thanks

0 Votes 0 ·