Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

ian izaguirre
ian izaguirre
3,220 Points

Put Myscript tag into my index.php of parent theme then added javascript file to wordpress child theme but not working ?

Hi I am new here to JavaScript, and I just started learning I use WordPress to create my websites. I created a javascript file called my script, then Put that script tag into my index.php of parent theme then added javascript file to wordpress child theme but that Javascipt file is not working ? I tested it by trying to change the color of a new div I added by using the document.getElementById("Myscript").style.backgroundColor = "#f0a" ---- But nothing is showing so I assume the index.php file script was not activated correctly ?

Any help would be great , thank you

5 Answers

Ok, seems that Alen got you a good enough answer for implementing your script. But your script still doesn't work properly. What you could do is:

  • first look for a nice script on the net which you copy/paste on your scrip, and test if the connection between your php file and your js file is working properly
  • if it's working properly with another script, then copy/paste your script on the forum (here or on the other) so we can look at it, maybe it's just your script which contain a small mistake.
ian izaguirre
ian izaguirre
3,220 Points

Ok before I put the code that I have inside the My.Script.JS file in my child theme, my question is if I have to put a line of code before in order to connect it to my functions php which then will make the JS file show the code on my website? Or does the code in my child themes functions php connect and "Activate" the Myscript.JS file solely through the Js files name "Myscript" , with no other lines of code needed in the myscript.JS file ?

I may be wrong (if someone more experienced thant me wants to correct me, please do so). Javascript doesn't interact with PHP, it interacts with the HTML. You Javascript can work without PHP. But maybe in the wordpress case, the script is called by PHP (it seems so, with the code you mention). And then, you don't need to add a response from Javascript to PHP to activate it.

ian izaguirre
ian izaguirre
3,220 Points

Hmm Im new to Java so for me your right ! :-) .... Inside my wordpress page which you can see here towards the bottom : http://izaguirre.me/contact/ I have this in html :

            <p><div id="myDiv">
These Are Radom Letters To Test The Connection Of the JAVA File DSFDFSFSDFF
</div></p>
            ``` 

And in my Myscript.js file the only code I have inside is this : 
        <p>

document.getElementById('myDiv').style.background = "black"; </p> ```

But I see no reflection of the JS working since there is no visible background change ?

Hi ian,

May you copy your entire code please? It is quite difficult to tell with just the script, your problem might be elsewhere. Maybe (but I can't really tell with this partial code), where did you put your <script> markup? In the end of the php file? Javascript need every element loaded to apply on it (later). If that's not the problem, we need a more global view of the entire code. Thanks :).

ian izaguirre
ian izaguirre
3,220 Points

Ok thank you. so inside my parent themes index.php file (ill paste the last couple of lines) at the very end I put :

            <p><!-- Finish Main Content -->
                    </div>    
                </div>


            </div> <!-- #main -->
        </div> <!-- #main-container -->




<?php get_footer(); ?>
<script src="myscript.js"></script></p>
            ``` 

- Inside one of my pages to test it I put:
 ```html
            <p><div id="myDiv">
words sdfdsf dfs fdsf fds
</div></p>
            ``` 

- My java script file thats in my child theme ( named myscript.js) I put : 
```html
            <p>document.getElementById('myDiv').style.background = "black";</p>
            ```
ian izaguirre
ian izaguirre
3,220 Points

I got a little further here: http://css-tricks.com/forums/topic/i-need-help-getting-my-javascript-file-to-work-on-wordpress-through-child-theme/#post-160138

But still need help answering the JavaScript question posted , if anyone can help me

ian izaguirre
ian izaguirre
3,220 Points

Codes are not showing correctly so this is the same post :

Ok thank you. so inside my parent themes index.php file (ill paste the last couple of lines) at the very end I put :

        <!-- Finish Main Content -->
                </div>    
            </div>


        </div> <!-- #main -->
    </div> <!-- #main-container -->

<?php get_footer(); ?> <script src="myscript.js"></script>

  • Inside one of my pages to test it I put:

        <div id="myDiv">
    

    words sdfdsf dfs fdsf fds </div>

  • My java script file thats in my child theme ( named myscript.js) I put :

        document.getElementById('myDiv').style.background = "black";
    
ian izaguirre
ian izaguirre
3,220 Points

these codes are not showing this is annoying lol and my edit button does not work for the post ?

Ok thank you. so inside my parent themes index.php file (ill paste the last couple of lines) at the very end I put :

'''<script src="myscript.js"> </script>'''

  • Inside one of my pages to test it I put:

<div id="myDiv">words sdfdsf dfs fdsf fds</div>

  • My java script file thats in my child theme ( named myscript.js) I put :

document.getElementById('myDiv').style.background = "black";

Please report your problem with your edit button (I did as well, I had the same problem, they are working on it by now, and the more infos they get, the quicker they will come with an answer).

(and I don't know why I didn't see your replies until now, and they are marked as 23 days ago...).

I can't help you well with this, it's a bit confusing with the replies not showed correctly.

ian izaguirre
ian izaguirre
3,220 Points

Hi Giovanni thanks for your reply, I know the edit button is messed up its frustrating , I re-wrote my problem with JavaScript here : http://css-tricks.com/forums/topic/i-need-help-getting-my-javascript-file-to-work-on-wordpress-through-child-theme/#post-160138

because the edit button is messed up on this forum hopefully it gets fixed. If you can help me by answering the question that's written clear on the link I posted and put your answer here that would be great. But if your unable to because of the forum issues I understand , Thank you

Hmm, is your file named "Myscript.js" or "myscript.js"? Your PHP function points toward myscript.js, but is the file correctly named? (cause you copied/pasted from Allen from the other forum).

ian izaguirre
ian izaguirre
3,220 Points

Hmm I just doubled checked the spelling of the Java file in the functions and in the child theme and they are both all lower case : "myscript.js" ... What else could it be ? I can also give you admin access if that would be easier for you ...

Actually I can't investigate any further, I'm sorry. Not that I don't want to, but I don't have the time right now, and someone more experienced could help you better and more quickly, I hope. I have no experience with wordpress so far, and I have some extra work for the two next weeks, it should not be reasonable to dispersate myself from it. I'm deeply sorry I couldn't help you.

ian izaguirre
ian izaguirre
3,220 Points

It ok I understand thank you

ian izaguirre
ian izaguirre
3,220 Points

Just letting you know I found out the fix to my issue on stack overflow if your interested, http://stackoverflow.com/questions/21124038/i-need-help-getting-my-javascript-file-to-work-on-wordpress-through-child-theme

Thanks for all of your help :-)

Glad to hear that you found an answer :).