0

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">
<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??

5 Answers 5

2

Perhaps you should enclose with double quotes:

<a href=\"javascript:sendDirectMessage('1711838', 'abc')\">
Sign up to request clarification or add additional context in comments.

Comments

2
  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

2 Comments

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.
5 is covered by 4: w3.org/TR/xhtml-media-types/#C_1 , but 6 is a good point.
1

Try this:

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

Comments

0

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.

Comments

0

As Aito says, you need double quotes:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.