How to add image to button via Javascript?
Asked Answered
C

6

7

I am trying to add image to buttons I have using Javascript but I can't seem to get it working. I have sucessfully added image using HTML5, however, for my needs I need to add the image via javascript.

Below is what I have written to add images via HTML.

<button class="button" id="Ok"><img src="images/ok.png"></button>

Below is what I have tried to add image via Javascript but it doesn't seem to work.

var buttons = document.getElementsByClassName("button");
for (var b = 0; b < buttons.length; b++) {
      buttons[b].src = "images\ok.png";   
  }

I am not to sure what I am doing wrong, any help would be nice. Thanks.

Catechize answered 2/3, 2016 at 8:43 Comment(1)
Nope, no luck. Getting this error: TypeError: Cannot set property 'src' of null.Catechize
H
16

I don't know if this is what you need..

<button id="button"></button>

<script type="text/javascript">
    var buttons = document.getElementById("button");
    buttons.innerHTML = '<img src="images\ok.png" />';
</script>
Henebry answered 2/3, 2016 at 9:18 Comment(3)
I have 1 issue, because its a image inside the button, it overlaps the button and when I click the section of the button where the image is, the website doesn't seem to record my clicks.Catechize
is it possible to give the img tag the same id as the button it is inCatechize
please update your question above so that we can trace the problem. put your code above also....Henebry
P
3

I think this may be what you're looking for.... this will set the buttons direct background as the image. But you must set the width and height options to your images width and height or it will be cut off or have white space, depending on the size of the button and image.

<style>
  button.button#ok
  {
    width:100px;
    height:100px;
  }
</style>
<button class="button" id="ok"></button>
<script>
  var buttons=document.getElementsByClassName("button");
  for(var b=0;b<buttons.length;b++)
  {
    if(buttons[b].id=="ok")
    {
      buttons[b].style.background="url('images/ok.png')";   
    }
  }
</script>

Edit, source

Here is the code in action:

http://js.x10.bz/helpstack/35742199/button.html

Here is the source of the code(which is above though):

http://js.x10.bz/helpstack/35742199/button.txt

And here is the image i used:

http://js.x10.bz/helpstack/35742199/cookies.jpg

Pneumoencephalogram answered 2/3, 2016 at 9:10 Comment(0)
P
1

I would turn the button into a block element and give it a background image.

HTML:

<button class="button" id="Ok"></button>

CSS:

.button{
display:inline-block;
background:none;
width: 50px;
height: 20px;
}
.button.okay{
background:url('images/ok.png');
}

JS:

var buttons = document.getElementsByClassName("button");
for (var b = 0; b < buttons.length; b++) {
 buttons[b].classList.add("okay");
  }
Potato answered 2/3, 2016 at 8:53 Comment(1)
Hi, tried this but its not working. Also, I want a way that only uses javascriptCatechize
B
1

Try this:

buttons[b].firstChild.src= "images\ok.png";
Bronchi answered 2/3, 2016 at 8:56 Comment(1)
Hi @Ankit Bhanderi, Rayon Dabre also suggested did but this provides me with an error: TypeError: Cannot set property 'src' of null.Catechize
U
1

I think you missing select tag , you only set src content for button element. Can you try with this

for (var b = 0; b < buttons.length; b++) {
   buttons[b].getElementsByTagName("img").src = "images\ok.png";   
}
Unkindly answered 2/3, 2016 at 9:4 Comment(5)
Can you try to console.log how many <img> it can get inside looping. for (var b = 0; b < buttons.length; b++) { buttons[b].getElementsByTagName("img").src = "images\ok.png"; console.log(buttons[b].getElementsByTagName("img")); } If it an empty array, we can try another wayUnkindly
What do you mean?, when I try console.log(b); it gives back 28 numbers this is because I have 28 buttons.Catechize
command "src" only apply for <img> tag. That why I'm try to get <img> tag inside <button>. May be some button do not have <img> tag, and looping will stop. On that case, you can put condition for that!Unkindly
Oh, none of the buttons have img tag on them. I thought it won't be required because I wasn't adding image via html.Catechize
So, you need something like this; var x = document.createElement("IMG"); x.setAttribute("src", "images\ok.png"); buttons[b].appendChild(x);Unkindly
B
1
**TRY THIS**

<!DOCTYPE html>
<html lang="en"><head>
</head><body>
  <button class="button" id="Ok"></button>
<script>
var buttons = document.getElementsByClassName("button");
for (var b = 0; b < buttons.length; b++) {
      buttons[b].innerHTML = "<img src=\"ok/png\"/>";   
  }
</script>
</body></html>
Bronchi answered 2/3, 2016 at 9:11 Comment(2)
Please Load Body Tag First Then Load Your Script Tag.Bronchi
Hi, this is initially adding the image through html <img src="images/ok.png"> because this buttons[b].firstChild.src = "images/ok1.png"; does not work.Catechize

© 2022 - 2024 — McMap. All rights reserved.