Simple JavaScript Swap function

93 views Asked by At

I'm having difficulty in correcting the syntax error for image swap. I would appreciate if you can help me out as below?

function swap(i, s)
{
   var d = document.images;
   d ? d[i].src=s : null;
}
1

There are 1 answers

2
deadboy On BEST ANSWER

I think you want something like this

function swap(i, s) {
    var d = document.images;
    if (i >= 0 && i < d.length) {
        d[i].src = s;
    }
}