www.webdeveloper.com
Results 1 to 3 of 3

Thread: Set src inside javascript

  1. #1
    Join Date
    Oct 2013
    Posts
    4

    Set src inside javascript

    I am trying to change a src from img tag inside javascript code. My html code is the following

    Code:
    <div>
    	<img id = 'op'  src = '2.png'>
    </div>
    and script code

    Code:
    	var image = 1;
    		var open = 'images/'+image+'.png';
    		console.log(open);
    		document.getElementById("op").src  = open;
    I got the message in the condole Cannot set property 'src' of null. Ant idea??

  2. #2
    Join Date
    May 2006
    Location
    Somewhere behind your screen
    Posts
    1,648
    maybe you are trying to set the element src attribute before the DOM is loaded?
    Last edited by Padonak; 10-24-2013 at 07:40 AM.
    xxx: Guess Buddhist riddle: "What is the sound of one hand clapping?"
    yyy: facepalm

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,154
    Try changing this
    HTML Code:
    <img id = 'op'  src = '2.png'>
    to
    HTML Code:
    <img id='op'  src='2.png'>
    and
    Code:
    var image = 1;
    		var open = 'images/'+image+'.png';
    		console.log(open);
    		document.getElementById("op").src  = open;
    to something like
    Code:
    window.onload = function(){
    var image = 1;
    	var open = 'images/'+image+'.png';
    	//console.log(open); <-- try not using this unless you need to.
    	document.getElementById("op").src  = open;
    }
    and see how you get on.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles