• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

What's the best way to add large html chunk (like a form and html etc.) using javascript?

Contributor ,
Oct 28, 2015 Oct 28, 2015

Copy link to clipboard

Copied

Hi all,

In php - we can do and if statement something like:

<?php if ($test1 !=''){

?>


add HTML HERE


<?php

// end if here

} ?>

and the html will only show if the condition is met.

Q: So, What's the best way to add large html chunk like a form using javascript IF statement or  a better way???

like this - or...???

<script>

if ( test1 !="" ){


//this....

document.getElementById('showthis').innerHTML = "large html chunk";

}


// or this....

$.get("codeblock-1.html", function(fileData){

$('put-stuff-here').html(fileData);

});

// or something better?

</script>

Views

596

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Oct 30, 2015 Oct 30, 2015

Copy link to clipboard

Copied

Hi all,


Someone mentioned using CSS hide which worked well but ultimately I found this:

http://api.jquery.com/hide/

And it really did the trick.... (& TREAT)!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 01, 2015 Nov 01, 2015

Copy link to clipboard

Copied

Using that is fine for hiding a form, but be careful about using it for elements that contain images. The jQuery hide() method sets the CSS display property to none, but all the content will still be downloaded.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Nov 09, 2015 Nov 09, 2015

Copy link to clipboard

Copied

I agree with David, I think you're better off making the conditional check first and actually loading the HTML after that.

I don't presume you're getting extremely deep into JavaScript content generation if you're dumping a lot of string data into the DOM but for the record, the DOM Fragment is a lean machine for generating any verbose content via JavaScript. Using it would even allow you to half the loading of images before they're needed. In short terms, every time you do "anything" to the DOM (like add content, any kind), the whole thing needs to reflow to take your changes into consideration. DOM Fragments can be created entirely on the side and do not cause this to happen, UNTIL you're done generating your DOM and you append it to the document.

It's may be a bit more of an advanced topic than you need but here's more info about performance and examples:

Document.createDocumentFragment() - Web APIs | MDN

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Dec 24, 2015 Dec 24, 2015

Copy link to clipboard

Copied

LATEST

THANKS SO MUCH DAVID & sinious !!!

YOU HAVE BEEN SO HELPFUL & I want to thank you for all your great advice!!!!

HAPPY HOLIDAYS!!!!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines