Solved

jQuery question

Posted on 2014-12-26
5
84 Views
Last Modified: 2014-12-26
I don't understand something.

$("#myDiv").text("<b>Boo</b>");

<div id="myDiv"></div>

Open in new window


The browser will actually display '<b>Boo</b>' without boldfacing the word 'Boo'. When I examine the source through the browser's debugger (e.g. webkit), the text doesn't show the output as using HTML entities like &gt; or &lt;, nor are any of the characters escaped in any way.

Can someone please explain to me what's happening here?
0
Comment
Question by:elepil
  • 2
  • 2
5 Comments
 
LVL 9

Assisted Solution

by:Brian Tao
Brian Tao earned 100 total points
ID: 40519289
In jQuery, using .text(str) will display exactly the str you assigned to it.  Please use .html(str).
0
 

Author Comment

by:elepil
ID: 40519291
Taoyipai, thanks for responding. I am aware about html(). My question is how come the text(string) output, despite obviously being escaped, is not showing in View Source or through the browser debugger.
0
 
LVL 58

Expert Comment

by:Gary
ID: 40519293
It won't show in view source because that is the rendered HTML at load, anything you add is in the browsers DOM

Jquery will html encode the < and > etc to their equivalent (&gt; or &lt;) but when you view it the debugger you will see the actual < >
0
 
LVL 58

Accepted Solution

by:
Gary earned 400 total points
ID: 40519294
If you use Firebug and select the element and click edit you will see the encoded characters
Whatever browser you are using for debugging will have similar.
0
 

Author Closing Comment

by:elepil
ID: 40519297
Thanks for all your help!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How  can  I extract  Id  from a  URL  using  Javascript? 12 42
JQuery Search Filter 2 71
Review of the nodejs code 1 38
Make checkboxes execute queries on click 8 36
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question