Jump to content

[SOLVED] jquery ajax script failing


stockton

Recommended Posts

I am attempting to use jquery/ajax to populate a web page with the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title> Skeleton </title>
<link rel="stylesheet" type="text/css" href="style/lines.css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
// JavaScript code here
function ItemSelected(File)
{
alert (File);
    $(document).ready(function() {
          $('#mybutton').click(function() {
              $('#message-paragraph').load(File);
     });
  });
}
</script>
</head>
<body bgcolor="#eeeeee">

<table width=100% border=0>
<tr align=center>
<td colspan=3>
<h1>

Help Pages 
<p></h1>
</td>
</tr>
<tr valign="top" align="left">
<td width=15% valign=top align=left class="rightborder">
<input type="submit" id=Grid value="Grid" onClick=ItemSelected("help-files/Grid.htm")><input type="submit" id=NodeListEditor value="NodeListEditor" onClick=ItemSelected("help-files/NodeListEditor.htm")></td>
<td width=85% valign=top>
<p id="message-paragraph">  Here is the body of the help file</p>
</td>
</tr>
</table>
<br />
<center>
<p>The page tail contents goes here if it is required</p>

</center>
</body>
</html>

Everything seems to work, including the alert in the script showing the correct file name, however I am failing to get message-paragraph populated with the contents of that file. Before someone asks, yes the required files do exist in the correct folder.

Please tell me what I missed.

Link to comment
Share on other sites

I am attempting to use jquery/ajax to populate a web page with the following code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title> Skeleton </title>
<link rel="stylesheet" type="text/css" href="style/lines.css" />
<script type="text/javascript" src="js/jquery-1.3.2.min.js"></script>
<script type="text/javascript">
// JavaScript code here
function ItemSelected(File)
{
alert (File);
    $(document).ready(function() {
          $('#mybutton').click(function() {
              $('#message-paragraph').load(File);
     });
  });
}
</script>
</head>
<body bgcolor="#eeeeee">

<table width=100% border=0>
<tr align=center>
<td colspan=3>
<h1>

Help Pages 
<p></h1>
</td>
</tr>
<tr valign="top" align="left">
<td width=15% valign=top align=left class="rightborder">
<input type="submit" id=Grid value="Grid" onClick=ItemSelected("help-files/Grid.htm")><input type="submit" id=NodeListEditor value="NodeListEditor" onClick=ItemSelected("help-files/NodeListEditor.htm")></td>
<td width=85% valign=top>
<p id="message-paragraph">  Here is the body of the help file</p>
</td>
</tr>
</table>
<br />
<center>
<p>The page tail contents goes here if it is required</p>

</center>
</body>
</html>

Everything seems to work, including the alert in the script showing the correct file name, however I am failing to get message-paragraph populated with the contents of that file. Before someone asks, yes the required files do exist in the correct folder.

Please tell me what I missed.

 

A couple of things:

 

1. $(document).ready() should be used at the beginning of your JavaScript code, and not within a function.  I'm not sure you even need to use it, in this instance, given what you're trying to do.

 

2. Look at your code closely.  Your function attempts to add an event handler function to the click event of an element with an id of myButton.  You don't have such an element.  Your function should simply look like:

 

function ItemSelected(file)
{
   alert(file);

   $("#message-paragraph").load(file);
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.