Solved

jQuery question

Posted on 2014-12-26
5
80 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
This article discusses how to create an extensible mechanism for linked drop downs.
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

937 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now