Matej Posted April 23, 2014 Share Posted April 23, 2014 Hello ,i have slide show : var i = 0; var path = new Array(); path[0] = "pic1.jpg" ; path[1] = "pic2.jpg"; path[2] = "pic3.jpg" ; function swapImage() { document.slide.src = path[i]; if(i < path.length - 1){ i++;} else {i = 0;} setTimeout("swapImage()",3000); } window.onload=swapImage; how do i set href for each pic?Thanks for answers Link to comment Share on other sites More sharing options...
justsomeguy Posted April 23, 2014 Share Posted April 23, 2014 It looks like you can set document.slide.href. Link to comment Share on other sites More sharing options...
Matej Posted April 23, 2014 Author Share Posted April 23, 2014 (edited) Wouldnt it set one href for all pictures? I'd like to set different href for all pictures Edited April 23, 2014 by Matej Link to comment Share on other sites More sharing options...
justsomeguy Posted April 23, 2014 Share Posted April 23, 2014 Then you either need to make another array for links, or make one array that contains both links and image sources. Link to comment Share on other sites More sharing options...
Matej Posted April 23, 2014 Author Share Posted April 23, 2014 (edited) like this? var i = 0; var path = new Array(); path[0] = "pic1.jpg" ; path[1] = "pic2.jpg"; path[2] = "pic3.jpg" ; var change=new Array();change[0]="www.just.com";change[1]="www.some.com";change[2]="www.guy.com";function swapImage() { document.slide.src = path[i]; document.slide.href=change[i];if(i < path.length - 1&& i<change.length-1){ i++;} else {i = 0;} setTimeout("swapImage()",3000); } window.onload=swapImage; Edited April 23, 2014 by Matej Link to comment Share on other sites More sharing options...
justsomeguy Posted April 23, 2014 Share Posted April 23, 2014 That's one way to do it. That's called parallel arrays. Link to comment Share on other sites More sharing options...
Matej Posted April 23, 2014 Author Share Posted April 23, 2014 Thanks for answer/advice/information Link to comment Share on other sites More sharing options...
davej Posted April 24, 2014 Share Posted April 24, 2014 I'm confused. What is document.slide? Link to comment Share on other sites More sharing options...
Matej Posted April 24, 2014 Author Share Posted April 24, 2014 <img src="devej.jpg" name="slide"> its like like if i set Id and than get it with var x. it would be x.src Link to comment Share on other sites More sharing options...
davej Posted April 24, 2014 Share Posted April 24, 2014 But can you assign an href to an <img>? I would think you would need to wrap the <img> inside an <a>. Link to comment Share on other sites More sharing options...
Matej Posted April 25, 2014 Author Share Posted April 25, 2014 (edited) well yea it should be like <a id="lol"><img src="davej.jpg" name="slide"></a>var i = 0; var path = new Array(); path[0] = "pic1.jpg" ; path[1] = "pic2.jpg"; path[2] = "pic3.jpg" ; var change=new Array();change[0]="www.just.com";change[1]="www.some.com";change[2]="www.guy.com";function swapImage() { document.slide.src = path[i]; var m=document.getElementById("lol");m.setAttribute("href","change[i]");if(i < path.length - 1&& i<change.length-1){ i++;} else {i = 0;} setTimeout("swapImage()",3000); } window.onload=swapImage; or? Edited April 25, 2014 by Matej Link to comment Share on other sites More sharing options...
davej Posted April 25, 2014 Share Posted April 25, 2014 <a href="#"><img src="#" id="slide"></a><script>var i = 0; var path = []; path[0] = "pic1.jpg" ; path[1] = "pic2.jpg"; path[2] = "pic3.jpg" ; var change=[];change[0]="www.just.com";change[1]="www.some.com";change[2]="www.guy.com";function swapImage() { var s=document.getElementById("slide");s.src = path[i]; s.parentNode.href = change[i];i++;if(i>=path.length || i>=change.length){i = 0;} setTimeout(swapImage,3000); } window.onload=swapImage;</script> Link to comment Share on other sites More sharing options...
Matej Posted April 25, 2014 Author Share Posted April 25, 2014 Well my code would work too , or wouldnt it?(cant test it atm) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now