Good day to all.

There is such a script:
It is necessary to make a change of pictures by clicking. I found an example on the net and wrote code on it. It does not work for me and displays an error in the console. Please tell me how can I fix the code? For a better understanding I will explain - .slide-225 is my class of pictures, and src are the paths of the images that I am trying to replace. I will be glad to any comment and advice :)

 $(".slide-225").bind("click", function() { var src = ($(this).attr('src="/wp-content/uploads/2017/01/Mirdif-2-960x640.jpg"') === "Mirdif-2-960x640.jpg") ? "Mirdif-full-960x640.jpg" : "Mirdif-2-960x640.jpg"; $(this).attr('src="/wp-content/uploads/2017/01/Mirdif-2-960x640.jpg"', src); }); 

    1 answer 1

    You do not correctly receive and write the attribute.

    For getting use record of this type: $(element).attr(attribute);
    An entry of this type is used for assignment: $(element).attr(attribute, value);

    In any case, the attribute name and its value is indicated in quotes, unless of course you specify the attribute name or its value by variable or by some calculations in JS

    Corrected version:

     $(".slide-225").bind("click", function() { var src = $(this).attr('src') ? "Mirdif-full-960x640.jpg" : "Mirdif-2-960x640.jpg"; $(this).attr('src', src); }); 
    • @ Webdev96 If this answer is the solution to your question, you should mark it as correct. - mix