tags:

views:

87

answers:

5

Hi everyone...

I've got an issue with jquery where I set the html of a div using the html method, but it does not set it correctly.

Here's the stripped down code I'm using:

<?xml version='1.0' encoding='utf-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html>
  <head>        
    <script language="javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js" type="text/javascript"></script>
    <script type='text/javascript'>
      jQuery(document).ready(function($) {
        $('#results').html("<div class='tweet'><a href=javascript:sendDirectMessage('1711838', 'abc')>DM</a><hr/></div>");
      })      
    </script>
  </head>
  <body>
    <div id="results"/>    
  </body>
</html>

but when I view the result in the generated page using firebug, the element has it's contents set to:

<a )="" abc="" href="javascript:sendDirectMessage('1711838',">DM</a>

What am I doing wrong??

+2  A: 

Perhaps you should enclose with double quotes:

<a href=\"javascript:sendDirectMessage('1711838', 'abc')\">
Aito
+1  A: 

Try this:

$('#results').html('<div class="tweet"><a href="javascript:sendDirectMessage(\'1711838\', \'abc\')">DM</a><hr/></div>');
Darin Dimitrov
A: 

You forgot to quote your href attribute.

Therefore, it stopped parsing the href's value after the first space.

You need to write the following:

$('#results').html(
"<div class='tweet'><a href=\"javascript:sendDirectMessage('1711838', 'abc')\">DM</a><hr/></div>"
);

The \" creates a " inside a double-quoted string.

SLaks
+2  A: 
  1. The HTML you are trying to generate is invalid (this is your primary problem). Since you are using XHTML, the rules for quoting attributes are simple: attribute values must be quoted. Yours are not.
  2. You are using JavaScript: pseudo URIs
  3. You are using invalid XHTML
  4. You are not using HTML-Compatible XHTML
David Dorward
5. You are including a redundant `<?xml` declaration, which does nothing except trip IE6-7 into Quirks Mode.6. The URL isn't valid even for a `javascript:` pseudo-URL (the space would need encoding to `%20`.7. You're using a javascript: pseudo-URL. David already said that, but I thought it was important enough to mention twice. Use event handlers like `$('a').click(function() { sendDirectMessage(...); return false; });` instead.
bobince
5 is covered by 4: http://www.w3.org/TR/xhtml-media-types/#C_1 , but 6 is a good point.
David Dorward
A: 

As Aito says, you need double quotes:

$('#results').html("<div class='tweet'><a href=\"javascript:sendDirectMessage('1711838', 'abc')\">DM</a><hr/></div>");
Frank Shearar