Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery DOM changes not appearing in view source

I have a simple question here. I know with jQuery you can dynamically append HTML elements in the DOM by doing stuff like

$('').append('<p>Test</p>');

But my question is, why don't these elements actually appear visually in the code (when you View Source in your browser for example).

Please experts, let me know why. Thanks

like image 469
user765368 Avatar asked Dec 22 '11 02:12

user765368


4 Answers

The original source never changes. Only the DOM changes.

You can see an HTML visualization of DOM changes using your browser's developer tools.

You should be aware that when you manipulate the DOM, you're never manipulating HTML. The HTML visualization offered by the developer tools is an interpretation of the current state of the DOM. There's no actual modification of HTML markup.

like image 111
user1106925 Avatar answered Nov 16 '22 08:11

user1106925


Because View Source only shows the HTML that was sent to the browser originally. There are ways of seeing the changed HTML though - Firebug in Firefox, F12 developer tools in IE or Chrome. Selecting some HTML and right-clicking "View Selection Source" in Firefox will also work.

like image 35
Michael Low Avatar answered Nov 16 '22 10:11

Michael Low


The "View Source" only shows the source the server sent at the time the browser requested the particular webpage from the server. Therefore, since these changes were made on the client side, they don't show up on the "View Source" because they've been made after the original page has been delivered.

To view the live source of the page, you can use the Web Inspector view in webkit browsers, or Firebug in Firefox. These keep track of any changes to the DOM and update the source which you can see.

like image 5
Indranil Avatar answered Nov 16 '22 08:11

Indranil


There is a option in web developer tool (Firefox addon) "View generated source" which will give you the whole source code which is generated after you made changes.

view source->View generated source
like image 3
Prasanth Bendra Avatar answered Nov 16 '22 08:11

Prasanth Bendra