Ask coding questions

← Back to all posts
changing image on click using JavaScript
ZControls_Games (18)

So every time someone clicks "Change Picture" I want it to change to the other pictures in my other files but I don't know how to do it with JavaScript.

Javascript instead of HTML

Geocube101 (590)

You're missing a comma on line 3
Anyway, just change the <img> source to the new file

Without JQuery
document.getElementById('fran').src = 'new_path';

With JQuery
$('#fran').attr('src', 'new_path');

Using Javascript to Change Picture Source

var pics = [

function change()
  var new_index = pics.indexOf(document.getElementById('fran').src) + 1;
  if (new_index > len(pics) - 1)
    new_index = 0;
  document.getElementById('fran').src = pics[new_index];
ash15khng (685)

@ZControls_Games Which method are you using? You may need to get jQuery if you are using it. Add this in your document's head: <script src=""></script>