Search code examples
jqueryselectorimagesrcattr

Can't I assign values from a $().attr() in the same line?


I'm trying to do this:

$('#smallpictureone').click(function () {
    $("mainproductpicture").attr("src") = $("smallpictureone").attr("src");
});

#mainproductpicture is the bigger picture on my page. So when someone clicks on the smaller one, the src of that smaller one should be set to the bigger picture.

Is something wrong with my code?


Solution

  • jQuery's attr() function uses the following syntax:

    $('yourselector').attr('attribute','value');
    

    For your purposes, using this may also help:

    $('#smallpictureone').click(function () {
        $("#mainproductpicture").attr("src", $(this).attr("src"));
    });
    

    There was also a # missing the the selector for mainproductpicture:

    $("mainproductpicture") should be $("#mainproductpicture");