0

I am trying to trigger a particular CSS on a particular ID, but it is effecting only one of the image of that ID, and not all the images with that ID. Also, how do I roll back the changes if I click again on the same trigger?

This is the HTML I am using.

<div class="row">
    <div class="container">
        <div class="col-sm-3">
            <img src="https://imagesonline.bl.uk/coo/user/gpimages/Images_Online_Cooliris_Logo.jpg" class="img-responsive" id="some" alt="">
        </div>
        <div class="col-sm-3">
            <img src="http://www.niemanlab.org/images/getty-images-logo.png" class="img-responsive" id="some" alt="">
        </div>
        <div class="col-sm-3">
            <img src="http://cdn.eso.org/images/thumb300y/eso1119b.jpg" class="img-responsive" id="some" alt="">
        </div>
        <div class="col-sm-3">
            <img src="http://www.niehs.nih.gov/news/newsroom/releases/2013/august19/tbbpa_image_.jpg" class="img-responsive" id="some" alt="">
        </div>
        <div class="col-sm-3">
            <img src="http://learningdesign.psu.edu/themes/site_themes/agile_records/images/uploads/LDImages/itsupport2.jpg" class="img-responsive" id="some" alt="">
        </div>
        <div class="col-sm-3">
            <img src="http://dummyimage.com/300" class="img-responsive" id="some" alt="">
        </div>
        <div class="col-sm-12 tac">
            <br><br><br><button>test</button>
        </div>
    </div>
</div>
hello world!

And this is the javascript I am using.

<script>
$('#image').click(function() {
    $('#some').css({
        'background-color': 'black',
        'color': 'white',
        'font-size': '44px',
        'opacity': '0.1'
    });
});
</script>
Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
Kristie Matthew
  • 275
  • 2
  • 4
  • 13

4 Answers4

2

The problem is you need to use UNIQUE ids. Instead use a class :

$('.img-responsive').css({
    'background-color': 'black',
    'color': 'white',
    'font-size': '44px',
    'opacity': '0.1'
});

Also, never use duplicate ids, IDs are meant to be unique throughout the document.

If you want to rollback the changes use classes instead. There are other approaches, but I feel this one is the best.

CSS:

.class1{
    background-color: black;
    color: white;
    font-size: 44px;
    opacity: 0.1;
  }

Jquery:

$('.img-responsive').toggleClass('class1');
connexo
  • 53,704
  • 14
  • 91
  • 128
Zee
  • 8,420
  • 5
  • 36
  • 58
  • Thanks @zee. Can you please tell me how do I roll back the changes? – Kristie Matthew May 23 '15 at 07:18
  • Will this be the final script I should be using? – Kristie Matthew May 23 '15 at 07:54
1

You should use class instead of id for multiple element. for roleback you can use Toggle Class | jQuery UI Try as below:

<style>
    .active{
        background-color: black;
        color: white;
        font-size: 44px;
        opacity: 0.1;
    }
</style>

JS

$('#image').click(function() {
    $('.img-responsive').toggleClass('active');
});   

Note: There should not be same id for multiple element in single page otherwise it will always consider first one.

Dhaval Bharadva
  • 3,053
  • 2
  • 24
  • 35
0

Try this:

$('#image').click(function() {
   $('.img-responsive').css({
   'background-color': 'black',
   'color': 'white',
   'font-size': '44px',
   'opacity': '0.1'
  });
}); 

And to rollback just Use this code:

$(".img-responsive").removeAttr("style");
Akash Rajbanshi
  • 1,553
  • 11
  • 23
0

To rollback to the previous state try using jquery's toggle method

http://api.jquery.com/toggle/

you can pass two functions in the toggle method as parameters and jquery will call each method one at a time.

jQuery click / toggle between two functions

check the link for more help and an example.

Community
  • 1
  • 1
Himanshu Tanwar
  • 906
  • 6
  • 18