Why is javascript closing all html tags directly

I try to build some html-code by a javascript. But somehow all html-tags I open get closed directly by the javascript. How can I change this behavior?

Example of javascript:

function generateHTML() { var s='<table>'; for(var i=0;i<max;i++) { s=s+'<tr><td>text</td></tr>'; } s=s+'</table>'; document.getElementById('tagID').innerHTML = s; }

The final html-code looks like:

<table></table> <tr><td>text</td></tr> : <tr><td>text</td></tr> </table>

--------------Solutions-------------

Save your final writing/appending of HTML until the end of your for-loop. Output your HTML more like this:

var s = '<table>';
for(var i=0; i<max; i++) {
s += '<tr><td>text</td></tr>'; // append to s
}
s += '</table>'; // append to s

console.log(s);

Output:

<table><tr><td>text</td></tr><tr><td>text</td></tr></table>

Demo: JSBin

Category:javascript Time:2018-12-23 Views:2

Related post

  • How to replace all html tags from to \n\n [using regexp (JavaScript)] 2010-08-31

    How to replace all HTML tags from <anything> to \n<anything> and </anything> to <anything>\n var text = "<anything>welcome</anything><anything>Hello</anything>"; result var text = "\n<anything>wel

  • Extract all html tag closed with a regex expresion 2012-03-23

    I work on R, and I will want to extract all HTML tag closed from a PlainTextDocument. I use a gsub method with a regex : gsub("<?!([^<]/*)>"," ",fm,perl=TRUE,ignore.case=TRUE) But, the slash '/' isn't evaluated. I think I wasn't very clear.

  • Remove all html tags and javascript tags using regex in javascript 2011-01-31

    How can I remove all html tags and script tags? Please consider also short tags like unclosed tags <script>blah...</script> <body> aaa<b>bbb</body> This should return aaa bbb notice that all the contents inside the scrip

  • Remove all html tags from attributes in rails 2010-04-05

    I have a Project model and it has some text attributes, one is summary. I have some projects that have html tags in the summary and I want to convert that to plain text. I have this method that has a regex that will remove all html tags. def strip_ht

  • Regex strip all html tags excluding & 2011-05-17

    I am relatively new to regex but my aim is to strip all html tags from a string excluding <br>s and <a> tags with the class='user'. I want to user this regex to clean unwanted html garbage from contentedittable fields. Hope one of you reg

  • Is generating Javascript Code and HTML Tags from HTMLHelper a Bad Practice? 2009-05-22

    maybe this is a stupid question, but i have seen many Tuts that build JavaScript ode and HTML Tags from an HTML Helper Extension Method like creating a Jquery Menu or Tree, it looks so neat and saves cluttering in View and encourage re usability. but

  • javascript replace allowed html tags 2011-05-20

    I need to use javascript to remove all html tags, except for those I explicitly allow. I have a form that only allows the following tags and their respective end tags: <b> <strong> <i> <em> <u> <br> <pre>

  • Is there any way to add namespaces to all html tags using ASP? 2011-06-01

    Is there any way to add namespace xmlns="http://www.w3.org/1999/xhtml" to all html tags either using ASP function / RegExp / javascript? (this is something like adding a attribute to the html tags) For Example: Below is the body of textarea: <p

  • PHP: How to convert single quote to double quote in all HTML tags? 2009-05-28

    How can I convert all single quotes to double quotes in all HTML tags only? Is there an easier way to do it? Thanks :) For example: How can I convert this string (actual data from my work): <TEXTFORMAT LEADING='2'><P ALIGN='LEFT'><FONT

  • Stripping all html tags with Html Agility Pack 2010-06-29

    I have a html string like this: <html><body><p>foo <a href='http://www.example.com'>bar</a> baz</p></body></html> I wish to strip all html tags so that the resulting string becomes: foo bar baz From ano

  • Count all HTML tags in page PHP 2010-07-06

    I spent time on regex to solve this problem but not have result i try solve this problem using PHP 5.3 Information like - How many times repeats in page and information about all tags in page. --------------Solutions------------- Your question is unf

  • python: How to remove all html tags from downloaded page 2010-07-28

    I have downloaded page using urlopen. How do I remove all html tags from it? Is there any regexp to replace all <*> tags? --------------Solutions------------- A very simple regexp would be : import re notag = re.sub("<.*?>", " ", html) Th

  • How to strip ALL HTML tags using MSHTML Parser in VB6? 2011-04-18

    How to strip ALL HTML tags using MSHTML Parser in VB6? --------------Solutions------------- This is adapted from Code over at CodeGuru. Many Many thanks to the original author: http://www.codeguru.com/vb/vb_internet/html/article.php/c4815 Check the o

  • Strip all HTML tags, except anchor tags 2011-12-02

    I'm importing data from 1 database to another. I've been requested to strip all HTML content, as its messy and not valid, and just keep the links I currently use the following VB.NET function to strip all HTML tags from a string of content: Public Sh

  • how to replace all html tag of a html file using PHP 2011-12-06

    I am extracting a file using $data = file_get_contents("file.html"); I want to replace all html tags from $data using a specified string (e.g : \n) . I can do this for a single line not for entire data . Thanks in advance. --------------Solutions----

  • php email script show all html tags 2012-04-04

    i am hosting my site on godaddy. my script send the email but it show all html tags as i don't know why it not converting the message into html format hear is my script $email = $row['email']; $mail_body = '<html> <body> <p>hello,'.

  • Regex to match all HTML tags and tag content except and 2009-03-20

    I am looking for a regex to match all HTML tags, except <p> and </p> that includes the tag content. I am developing in ColdFusion. There was an earlier post about matching tags except <p> and </p>, but I need to grab everythin

  • how to get the contents inside a div along with all html tags along with values present in text box etc tags 2009-12-14

    how to get the contents inside a div along with all html tags along with values present in text box etc tags . The jQuery function $('#divid').html() is working in IE but the firefox i am getting the html but the values which are in text area and tex

  • How do I extract all HTML tags from a webpage into an array? 2010-10-06

    I need to extract all HTML tags from a webpage into an array without the data inside the tags. It would look something like... I'm using PHP Array { html => Array { head => Array { title, meta name='description' content='bla bla' meta name='key

  • Parsing newline characters in textareas without allowing all html tags 2010-11-23

    I have a textarea field where users can enter content. When it comes to displaying their entry on a page, rails returns \n for each line break, which appears as no break at all for html on the page. From what I gather, the standard way of getting aro

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.658 (s). 13 q(s)