JPate
JPate

Reputation: 141

Script won't run within WordPress plugin

I'm developing a very simple plugin that calculates a total of fields based on user input.

The plugin displays a form that calls an associated javascript. I began by creating one file (script + html form) which runs fine in my local WP div site, but when I "split it" into the script file and the html file, the form will not calculate within WP. I verified the script is running on the page, but the form does not calculate.

Here's a snippet of the script:

<script language="Javascript"><!--
function addUp(form) {
var LR_bookcaseV;
LR_bookcaseV =parseInt(document.spacecalc1.LR_bookcase.value);
if (isNaN(LR_bookcaseV) || LR_bookcaseV == "") {
    LR_bookcaseV = 0;
}

var APP_aircondV;
APP_aircondV =parseInt(document.spacecalc1.APP_aircond.value);
if (isNaN(APP_aircondV) || APP_aircondV == "") {
APP_aircondV = 0;
}
-->

Here's a snippet of the form:

<form name="spacecalc1">
<table border="0" cellpadding="2" cellspacing="0" frame width="100%" height="850">
<tr> 
<td width="107" valign="bottom" style="border-style: solid; border-width: 1"> 
<div class="maintext"> <font size="1" face="Verdana, Arial, Helvetica,sans-serif">       
<b>Living Room</b></font></div></td>

The JsFiddle: http://jsfiddle.net/p8j3T/2/

It doesn't like jsfiddle but, again, this will run on a page as ONE file, with the script preceeding the form, within WordPress.

Help appreciated!

Upvotes: 1

Views: 254

Answers (1)

Jos
Jos

Reputation: 1484

You probably copy the script tags from the javascript when you use the script in an external file. Remove those and you're good. I've updated the fiddle so it works to demonstrate: http://jsfiddle.net/p8j3T/3/

remove:

<script language="Javascript"><!--

and

// -->
        </script>

---EDIT---

answers for the comments below:

A: (this one will make sure jquery is loaded also)

<?php wp_enqueue_script(
    'default',
    get_stylesheet_directory_uri() . '/js/default.js',
    array( 'jquery' )
); ?>

B:

<script src="<?php bloginfo('template_url'); ?>/js/your-js-file.js"></script>

Upvotes: 2

Related Questions