Mohamad Arafat
Mohamad Arafat

Reputation: 573

Uncaught TypeError: Cannot set property 'src' of null when want to switch the image

I am keep getting the Uncaught TypeError: Cannot set property 'src' of null error when i want to switch the image.

I am using this 2 files for jquery

<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.5.2/jquery-ui.min.js"></script>

This the javascript code

function correctcheck() {

    var value = document.getElementById("codes").value;
    var img = document.getElementById("gameplay");
    if (value == "$player //Declare player as an object\n\n$player = 'Artix' //Set player to Artix\n\necho = 'I am'.$player.'I got the key' // Echo the message\n\n") {
        img.src="correct.gif"; //error is here
        window.alert("Congratulation. You passed forest level.");


   return false;

    } else {
        window.alert("Your Drag and drop code is incorrect..View tips to get correct answer.");
    }
}

This is my html code for the img src

    <div class="featured">
        <div class="wrap-featured grid">
            <div class="slider">
                <img  src="Artix.png" alt = "GameBanner" name="gameplay" id "gameplay"/>
                <textarea name="codes" id="codes" class="txtDropTarget" cols="80" rows="10"></textarea>
                <a class="button" onClick="correctcheck()"  >Run</a>

            </div>
        </div>
    </div>

Upvotes: 0

Views: 1712

Answers (2)

Hossein Akbarzadeh
Hossein Akbarzadeh

Reputation: 344

you must be insert "=" element in you're img tags like this:

<img  src="Artix.png" alt = "GameBanner" name="gameplay" id="gameplay"/>

Upvotes: 3

Hossein Akbarzadeh
Hossein Akbarzadeh

Reputation: 344

you must be insert $(document).ready in you're code and "=" element in you're img tags like this:

<img  src="Artix.png" alt = "GameBanner" name="gameplay" id="gameplay"/>

function correctcheck() {
       $(document).ready(function(){
           var value = document.getElementById("codes").value;
           var img = document.getElementById("gameplay");
                  if (value == "$player //Declare player as an object\n\n$player = 'Artix' //Set player to Artix\n\necho = 'I am'.$player.'I got the key' // Echo the message\n\n") {
                    img.src="correct.gif"; //error is here

                    window.alert("Congratulation. You passed forest level.");
                    return false;
                } else 
                    {
                   window.alert("Your Drag and drop code is incorrect..View tips to get correct answer.");
                   }}
       )};

Upvotes: 0

Related Questions