cs.punk Posted February 23, 2011 Share Posted February 23, 2011 Can anyone tell me if there is any difference between: function changeImgOn(picID, imgSrc) { var img = document.getElementById(picID); img.src = imgSrc; } or function changeImgOn(picID, imgSrc) { var img = document.getElementById(picID); img.setAttribute("src", imgSrc); } Where my HTML is: <img id='pic1' src='home_off.jpg' onmouseover='changeImgOn("pic1", "home_on.jpg")' onmouseout='changeImgOn("pic1", "home_off.jpg")'/> They both work but just wanted to know. Thank you! Quote Link to comment https://forums.phpfreaks.com/topic/228651-newbie-question-about-getelementbyid/ Share on other sites More sharing options...
codefossa Posted February 24, 2011 Share Posted February 24, 2011 They would both do the same thing essentially. Why do you create a var when you're only using it one time though? Also, this can be accomplished in JQuery much easier with $("#picID").val(imgSrc); Quote Link to comment https://forums.phpfreaks.com/topic/228651-newbie-question-about-getelementbyid/#findComment-1179001 Share on other sites More sharing options...
haku Posted February 24, 2011 Share Posted February 24, 2011 Actually this doesn't need to be done with javascript at all, and should be done in CSS. Quote Link to comment https://forums.phpfreaks.com/topic/228651-newbie-question-about-getelementbyid/#findComment-1179002 Share on other sites More sharing options...
codefossa Posted February 24, 2011 Share Posted February 24, 2011 Actually this doesn't need to be done with javascript at all, and should be done in CSS. True, but I was just answering the question. Quote Link to comment https://forums.phpfreaks.com/topic/228651-newbie-question-about-getelementbyid/#findComment-1179010 Share on other sites More sharing options...
RussellReal Posted February 24, 2011 Share Posted February 24, 2011 haha could be done in CSS Completely like HAKU said, I'll show you an example (without CSS:background-image hacks or CSS:background-position hacks) <style type="text/css"> a img { border: none; } a .dn { display: none; } a:hover .dn { display: block; } a:hover .up { display: none; } </style> <a href="link.html"> <img class='up' src="linkUp.gif" /> <img class='dn' src="linkDn.gif" /> </a> oh and to answer your question.. You're most likely better off using .src as setAttribute might not be supported by all browsers Quote Link to comment https://forums.phpfreaks.com/topic/228651-newbie-question-about-getelementbyid/#findComment-1179012 Share on other sites More sharing options...
cs.punk Posted February 24, 2011 Author Share Posted February 24, 2011 Thank you! As for the var, was just testing code as such. As for using css, I guess but I find javascript is allot easier to work with. What is the worst that can happen? JS turned off and the rollovers don't work? Quote Link to comment https://forums.phpfreaks.com/topic/228651-newbie-question-about-getelementbyid/#findComment-1179041 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.