Solved

jQuery question

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
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)

861 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