Wednesday 18 January 2017

Update data attribute value using jquery


Set Attribute Value Using Jquery

$('.className img').data('block', 'something');
$('.className img').attr('src', 'something.jpg');


OR


$('.className img[data-id="4"]').attr('data-block', 'something');
$('.className img[data-id="4"]').attr('src', 'something.jpg');


Update many image elements. You want to update the value of specific image by using data-id

$('.className img').each(function(index) {
    if($(this).attr('data-id') == '5')
    {
        $(this).attr('data-block', 'something');
        $(this).attr('src', 'something.jpg');
    }
});

OR

$('.className img[data-id="5"]').attr('data-block', 'something');
$('.className img[data-id="5"]').attr('src', 'something.jpg');

No comments:

Post a Comment