Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
107
rated 0 times [  110] [ 3]  / answers: 1 / hits: 59262  / 8 Years ago, wed, march 2, 2016, 12:00:00

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 = imagesok.png;
}


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


More From » html

 Answers
17

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=imagesok.png />';
</script>

[#63088] Monday, February 29, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kevonmoisesf

Total Points: 693
Total Questions: 101
Total Answers: 128

Location: Reunion
Member since Mon, Dec 28, 2020
3 Years ago
;