How can I check the checkboxes according to the value using jQuery?

How can I check the checkboxes (when the page loads) according to the value of the checkboxes using jQuery? I have a list of values ​​that I saved in localstorage (Long live HTML5!). Data samples will be

something_random|something_random2|something_random3

And basically, I want to read each value and make sure that the onload flags that already have these values ​​are checked.

I already wrote:

my_cookie=localStorage.getItem('chosen_ones');

      $.each(my_cookie.split("|"), function(index, values) {
               if((values==val)&&(values!=null)&&(values!="")&&(values!="null")&&(values!="")){
                 $('input[value='+values+'').attr('checked', true);
            }
        });

However, this did not work. How can i fix this?

+3
source share
1 answer

I would suggest something like:

$('input[type=checkbox][value='+values+']').attr('checked', true);

Note the type and closure of ']'.

+3
source

Source: https://habr.com/ru/post/1762492/


All Articles