-1

I am trying to use jQuery to create HTML elements. It seems easier just to create the HTML elements using HTML code. However, the assignment I'm working on says to create my elements using Javascript/jQuery.

So my question is, if I create html elements in jQuery, are they added to my HTML file? If not, where does it go?

6
  • 1
    you might want to research on jQuery .append() w3schools.com/jquery/html_append.asp Commented Jan 8, 2016 at 7:38
  • It is added to DOM (in browser memory) and doesn't persist in file, because of Javascript security. Commented Jan 8, 2016 at 7:38
  • oh I see... so is there a way to view the DOM? Commented Jan 8, 2016 at 7:40
  • use your web browsers built in inspector. Right click > inspect element. Commented Jan 8, 2016 at 7:44
  • wow. Thank you so much. This will take me pretty far. Thanks again! Commented Jan 8, 2016 at 7:49

1 Answer 1

0

If you will add any new element using JavaScript/jQuery then it will not be displayed in view source but if you want to know that new element is added or not then you can check it using inspect element.

Below is code to check it programatically.

<script>
if($("div.container").children("div#new-div").length == 0)
{
     // New Div has not been added
}
else
{
    // New Div has been added successfully.
}
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

View source is IMHO not really helpful for developing. Better use developer tools (as in safari, chromium, google chorme) or Inspector in Firefox. In the developer tools you can see html tags being added dynamically
You are right, I must have overlooked it because you named it as inspect element. Note that not all (IE) browsers have an Inspect element in their menu
oh wow ok. I didn't know that you can use .length like that. Using it to find the number of children. I mainly used it to find the number of characters. Interesting.
If it's interesting then this answer should be up voted & approved.

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.