There is a video where the same code works normally, but I don’t. What is the bug?

<img src='1.ipg' alt='Малюки' title='Малюнки'><br> <img src='2.ipg'><br> <img src='3.ipg'><br> <img src='4.ipg' alt='Малюнки' title='Малюнки'><br> 
 <script type='text/javascript'> $(':not(img)[alt])').attr('alt', 'jQuery'); </script> 

Closed due to the fact that off-topic participants Grundy , Alexey Shimansky , cheops , aleksandr barakin , zRrr Jun 17 '16 at 14:21 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • "The question is caused by a problem that is no longer reproduced or typed . Although similar questions may be relevant on this site, solving this question is unlikely to help future visitors. You can usually avoid similar questions by writing and researching a minimum program to reproduce the problem before publishing the question. " - Alexey Shimansky, cheops, aleksandr barakin, zRrr
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    you do not have in the markup of elements that do not have pictures that have the alt attribute - Grundy
  • Maybe jQuery is not connected? - nick_n_a
  • no connected youtube.com/… here for 4 min all the rules work - yethyd
  • @Grundy, somehow ashamed to answer this question even. $(document).ready(function() {...} not registered, jquery not connected - Mr. Black
  • one
    @ Alexey Shimansky, In any incomprehensible situation, look at the console. © Alexey Shimansky - Mr. Black

2 answers 2

It is worth analyzing which elements this selector selects and which elements should be selected.

Analyze the selector

 :not(img)[alt] 

It consists of two parts.

  1. selector : not - selects all elements that do not satisfy the selector, in this case :not(img) selects all elements that are not img

  2. selector by attribute - selects all elements that have a specified attribute, in this case: [alt] selects all elements that have an alt attribute

Together, these selectors will return: all elements are not img , which have an alt attribute

Judging by the markup provided, a sample of all img elements was expected without the alt attribute, this corresponds to the following selector:

 img:not([alt]) 

 $('img:not([alt])').attr('alt', 'jQuery'); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <img src='1.ipg' alt='Малюки' title='Малюнки'><br> <img src='2.ipg'><br> <img src='3.ipg'><br> <img src='4.ipg' alt='Малюнки' title='Малюнки'><br> 

Do not watch the video, on it, except for the pictures, the alt attribute has also been added to the html, head, body and all other tags

  • Nishtyakova answer, plus - Mr. Black

Perhaps you forgot to put an event in which this script should work?

eg

 <script type="text/javascript"> $( document ).ready(function() { $(":not(img)[alt])").attr ("alt", "jQuery"); }); </script> 

Or do you apply code to a page where there are no matching elements?

  • The message was published as an answer, but it does not contain an answer to the question. The message may be a comment, a comment, another question, but perhaps it should just be deleted. - Grundy
  • youtube.com / ... 4th minute - yethyd