0

I am trying use this function that changes the class and onclick of another element.

partner = this.id + 'Content';

            document.getElementById(partner).className = 'newswrapper';
            document.getElementById(partner).onclick = function(){ clickNews(this); } ;

the id of the element with the onclick looks like this.

echo'<div id="newsSlide'.$counter.'" class="newsimage" 
            onclick="clickNews(this)" 
            style='."'".'background-image:url("../img/'.$row['image'].'")'".'>';

and the element that is supposed to have its class changed looks like this

echo'<div id="newsSlide'.$counter.'Content" class="newswrapper">';

getting the error Uncaught TypeError: Cannot set property 'className' of null

echo'<script>';
        echo"function clickNews(id)
        {
            var y = document.getElementsByClassName('newswrapper');
            var x = document.getElementsByClassName('newswrapper2');

            var length = y.length,
            element = null;
            for (var i = 0; i < length; i++) {
              element = y[i];
              element.className = 'newswrapper2';
            }

            var length = x.length,
            element = null;
            for (var i = 0; i < length; i++) {
              element = x[i];
              element.className = 'newswrapper2';
            }

            partner = this.id + 'Content';

            document.getElementById(partner).className = 'newswrapper';
            document.getElementById(partner).onclick = function(){ clickNews(this); } ;

        }";
        echo'</script>';

the html can be found here : http://pastebin.com/6xBNBQwZ or you can use the HTML inspector on the actual page here here : http://www.uk-sf.com/indextest.php

0

2 Answers 2

5

You're missing document.:

document.getElementById(partner).className = 'newswrapper';

getElementById is a method of the document, not the window ;)

Sign up to request clarification or add additional context in comments.

3 Comments

uk-sf.com/indextest.php Here is where I am testing the function, it isn't working still.
getting the error Uncaught TypeError: Cannot set property 'className' of null
All of them work except the last one. This suggests that the last one does not have a corresponding element.
0

Your this.id in partner = this.id + 'Content' is refer to global variable id on window object not the id to your element, and becouse ther is not global variable the partner variable looks like that undefinedContent

So change onclick="clickNews(this.id)" and than use id from arguments

function clickNews(id) 
    {
        partner = id + 'Content'; // use id form arguments
    }

And fiddle with simple example

3 Comments

solutions? change the onclick to onclick="clickNews(this.id)". And then adapt the function to use the id????????
sorry could you present me more of a solution, I have been ironing out my PHP ability but I still really am learning with the javascript
I put a fiddle with simple example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.