javascript not working in templates

0

Hey guys,

I'm having trouble running javascript inside Pods page templates - even a simple page template like this doesn't work:

<?php
/*
Template Name: Tester
*/
?>

<script type="text/javascript" charset="utf-8">
    document.getElementById('testdiv').click(function(){
        alert('test worked!');
    });
</script>

<style type="text/css" media="screen">
    #testdiv { width:100%; height:100%; background:#ccc; }
</style>

<div id="testdiv" >
    this is a test
</div>

Can anyone shed any light?

cheers

asked Sep 12 '10 at 9:17

urban232

8

edited Sep 12 '10 at 9:17

add comment
enter at least 15 characters

12 Answers

0

First thing you need to do is to change the code, because it's half-jQuery, but it won't be recognized without '$' or 'jQuery'. So

jQuery('#testdiv').click(function(){ your code });

The second thing - declare your script after you've placed 'testdiv' on a page, jQuery has never worked with elements placed after the script on my site.

answered Sep 13 '10 at 2:32

Fike

208

add comment
enter at least 15 characters
0

Your javascript is not correct; Are you trying to use jQuery or plain javascript (without a library)?

answered Sep 12 '10 at 2:07

clarinetlord

456

add comment
enter at least 15 characters
0

aah - I am new to JS... but thought I had that right.

I'd like to be using jQuery but that wasn't working either - what am I doing wrong?

<?php
/*
Template Name: Tester
*/
?>

<script type="text/javascript" charset="utf-8">
    jQuery('#testdiv').click(function(){
        alert('test worked!');
    });
</script>

<style type="text/css" media="screen">
    #testdiv { width:100%; height:100%; background:#ccc; }
</style>

<div id="testdiv" >
    this is a test
</div>

answered Sep 13 '10 at 2:24

urban232

8

add comment
enter at least 15 characters
0

Excellent - thanks for the tip Fike!

answered Sep 14 '10 at 3:46

urban232

8

add comment
enter at least 15 characters
0

im tryin to add JS to a POD PAGE, it fires normal Javascript but its not rendering the above codes i have no clue why even the error console has no errors shown? i used this --> doesnt work

jQuery('#testdiv').click(function(){
        alert('test worked!');
    });

instead used onClick = functionCall(); this works fine

so jquery somehow doesnt work?

answered Sep 14 '10 at 7:24

acenik

1

add comment
enter at least 15 characters
0

like this? Can anyone please tell me why this doesn't work?

<?php
/*
Template Name: tester
*/
?>

<?php get_header(); ?>
<div id="main" >
    <div id="content" >
        <script type="text/javascript" charset="utf-8">
            jQuery(document).ready(function(){
                jQuery('#test').css({'background', '#ccc'});

            });
            setTimeout(alert('works'), 1000);
        </script>
        <div id="test" style="width:100%; height:100%; border: 1px solid #000;"></div>
    </div>
    <!-- end content -->
</div>

answered Sep 14 '10 at 12:55

urban232

8

add comment
enter at least 15 characters
0

I may be wrong about this, but maybe jQuery('#test').css({'background', '#ccc'}); should really be jQuery('#test').css('background', '#ccc'); since css() isn't looking for an object here.

answered Sep 14 '10 at 1:49

sc0ttkclark

2936

add comment
enter at least 15 characters
0

thanks for the suggestion but that doesn't work either - it seems that nothing written in a script block within a div works - eg. setTimeout(alert('works'), 1000);

Something I'm missing?

answered Sep 14 '10 at 2:38

urban232

8

edited Sep 15 '10 at 1:42

add comment
enter at least 15 characters
0

jquery doesnt seems to work, normal core JS works fine, i see the library being included in the source in the wordpress header, do we need to re-include it in pods, if yes may i please have the proper syntax?

answered Sep 15 '10 at 4:57

acenik

1

add comment
enter at least 15 characters
0

No, you don't need to include it twice, it should work if you've ever placed it on a page. I have working pod pages with jQuery AJAX and tab menu.

update: but of course, you need to include wordpress header on pod pages too, or include all js and head stuff you need manually.

answered Sep 15 '10 at 7:55

Fike

208

edited Sep 15 '10 at 8:00

hey what do you mean by include the wordpress header in pod pages? you mean make a call to the header? <?php get_header(); ?> please paste a sample of ur pod page? thanks! – acenik Sep 16 '10 at 9:28
add comment
enter at least 15 characters
0

figured it out - it was due to the fact I was loading the page in with jQuery.load() - which strips out all inline JS!!!

answered Sep 16 '10 at 2:52

urban232

8

add comment
enter at least 15 characters
0

@acenik Just to mention - i have two headers in my own theme - one is just title, js, css & stuff, the other one contains visible menu, headerpic, etc. Most of pod pages are opened through Shadowbox JS, so they contain the invisible data header.

<?php
/*
Template Name: pods_plugin
*/
?>
<?php get_header(); ?>
<div class="plugin">
<?php
    %getting lots of fields%
    %sanitizing lots of fields%
?>
<div class="plugininfo">
%all the output%
</div>
<?php include('pods_comments.php'); ?>
</div>
<?php get_footer(); ?>

nothing special, as you see; it is just important to include header and footer for correct wordpress work (for example, all jQuery scripts from plugins will attach to the footer hook, so they won't work if you won't incude the footer).

p.s. yes, i attach those templates as theme templates

answered Sep 16 '10 at 6:56

Fike

208

edited Sep 16 '10 at 7:34

add comment
enter at least 15 characters