Code Bunny
Code Bunny

Reputation: 447

HTML converter in javascript problems

I am currently writing a text to HTML converter that's supposed to:

Get input from text box 1.

Split that input into the elements of an array by paragraph. (I define the paragraph by anything that has text and ends with /n)

Iterate through the array and add <p style=\"font-size: 13px; text-indent: 15px;\"> to the front of every element and </p> to the end.

Get input from text box 2. (supposed to be single line)

add <p style=\"font-size: 11px; color: #666666; text-align: left; margin-top: 30px;\">A Power News :> in front and </p> to the end.

Output both of the into a text box.

The code I have written is as follows:

HTML:

<form name="input" method="get">
Input the text here:<textarea cols="40" rows="5" name="bodyText" id = "bodyText" > </textarea><br />
Input Author Name:<input type="text" name="authorName" id = "authorName" />
<button type="button"value="Convert" onclick="convertBlog()">Click to convert to html template</button>
</form>
<textarea cols="110" rows="40" name="output" id= "output">  </textarea>

JavaScript:

//STATIC VARIABLES USED FOR THE START AND END OF THE HTML LINES
var blogParagraphStart = "<p style=\"font-size: 13px; text-indent: 15px;\">";
var authorParagraphStart = "<p style=\"font-size: 11px; color: #666666; text-align: left; margin-top: 30px;\">A Power News 記者: "
var paragraphEnd = "</p>"

//GET THE INPUT FROM THE USER AND ASSIGN THEM TO VARIABLES
var bodyTxt = document.forms["input"]["bodyText"].value;
var authorName = document.forms["input"]["authorName"].value;

var bodyArray=str.split("\n"); 


var end = bodyArray.length+1;

while (i<end){
bodyArray[i] = blogParagraphStart + bodyArray[i] + paragraphEnd;
i++;
}

var reporterOutputString = authorParagraphStart + authorName +paragraphEnd ;

var outputString = bodyArray+ reporterOutputString;

document.getElementById("output").innerHTML= outputString ;

For some reason this doesn't work the button does nothing and I haven't been able to figure this out.

I hope I clearly explained my situation. This converter is only for a very specific use so re-usability is not a concern.

Upvotes: 1

Views: 185

Answers (2)

Edd Slipszenko
Edd Slipszenko

Reputation: 396

Have you opened up your javascript console to look for any errors?

I don't believe you've initialised:

i = 0;

before using it in the loop, so it might be complaining about that.

Upvotes: 1

brains911
brains911

Reputation: 1310

Are you getting any javascript errors in the console? I'm not seeing in your code where "str" is defined from the line var bodyArray = str.split("\n");

Also it seems since bodyArray is an array you would need to call join() on it before throwing it into a string?

Also, i is never declared for your while loop.

Upvotes: 1

Related Questions