1

Hi i got a script like this:

$(".news-button").click(function(){
    var img = document.getElementById('news-image').src;
    if (img.indexOf('news-bt-1.png')!=-1) {
        document.getElementById('news-image').src  = 'img/news-bt-2.png';
    }
    else {
        document.getElementById('news-image').src = 'img/news-bt-1.png';
    }

});

It worked fine but now i want to change news-image from id to class so how to change the code above?

Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
Lee
  • 1,041
  • 7
  • 20
  • 31

6 Answers6

1

You can use getElementsByClassName instead getElementById

CodeWithCoffee
  • 1,896
  • 2
  • 14
  • 36
0

Use :-

document.getElementsByClassName('news-image')[0].src

Since getElementsByClassName returns array you need to use index of it.

But as i can see you are using jquery on click event then why you are using javascript inside it instead use jquery inside it will help you in cross-platform

$(".news-image").each( function() {
   var img= $(this).attr('src');
 if (img.indexOf('news-bt-1.png')!=-1) {
           $(this).attr('src','img/news-bt-2.png') 
  }else{
  $(this).attr('src','img/news-bt-1.png') 
  }

});

squiroid
  • 13,809
  • 6
  • 47
  • 67
0

You can use

var elements = document.getElementsByClassName(classname)

It will get you all the elements matched as an array. So first make sure you got the array, then access the elements like elements[index]

0

You can use getElementsByClassName and select the first element, because this method returns a list of Elements which is ordered by the position of the elements in the DOM.

So, given that the element you want to get is the first one that appears in your HTML code with that class, you can do:

document.getElementsByClassName("news-image")[0]

And if it isn't the first one, you can change the number and select another element:

document.getElementsByClassName("news-image")[2] // third element

You can also use querySelector to select the first element that matches the given CSS selector, which is, in my opinion, easier:

document.querySelector("img.news-image")

Finally, since I see you're using jQuery as well, you can use it's selector like this:

$("img.news-image")[0]

Talking about your specific code, here's the solution you were looking for:

$(".news-button").click(function(){
    var img = $('img.news-image')[0];
    if (img.src.indexOf('news-bt-1.png')!=-1) {
        img.src  = 'img/news-bt-2.png';
    }
    else {
        img.src = 'img/news-bt-1.png';
    }
});
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • @LuciferLee you're welcome. If my answer solved your question, I suggest you to mark it as correct so that future users with the same problem will find it easily. – Marco Bonelli Mar 14 '15 at 07:32
0

Use the below code, just Replace className with the actual class name of you tag img :

$(".news-button").click(function () {
        var img = document.getElementById('news-image').src;
        var img1 = document.getElementsByClassName("className");
        if (img.indexOf('news-bt-1.png') != -1) {
            img1.src = 'img/news-bt-2.png';
        }
        else {
            img1.src = 'img/news-bt-1.png';
        }

    });
Vikrant
  • 4,920
  • 17
  • 48
  • 72
0

You can use Jquery as well.

To get source of image:

$('.classname').attr('src')
Yigit Yuksel
  • 1,060
  • 2
  • 18
  • 35