PDA

View Full Version : Simple Text to HTML formater



The Tao Of Bill
03-08-2007, 05:47 AM
I guess it's been too long since I've brushed up on my Javascript because something that seemed simple became very difficult very fast.

Basically I am trying to create a formater in javascript. I post listings on ebay for a living and part of the job involves copying a few lines of description and pasting it into the ebay description and formating it to be seperated by lines with a dash before it. The original description has things seperated by commas and I want to be able to copy the whole thing and input it into the program and have it come out in html with a dash before it and a line break at the end. This would cut my work time in half. I know it is very possible in Javascript. In fact I did something like this before. But I just can't seem to get it to work this time. This is the code I made.



<!-- HIDE FROM INCOMPATIBLE BROWSERS
function Jumble()
{
var input = document.forms[0].description.value;
var split = input.split(", ");
var output = "";
for(i=0; i < split.length; i++;)
{
if( i != 1 - split.length)
output = output + "-" split[i] + "<br />\n\n";
else
output = output + "-" split[i] + "<br />";
}
document.forms[0].description.value = output;
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>

<body>

<form >
<p>Description Formater<br />
<textarea name="description" cols="50" rows="30" /></textarea> </p>
<p><input type="button" value="Format" onclick="return Jumble();" /> </p>
</form> Everything looks fine to me but when I run it I get an error in the java console saying the Jumble function is not defined. But as far as I can tell it is defined. I am guessing there is something wrong with the code within the function. But I can't really figure out what I did wrong. I'm just really rusty I guess. Can someone help me out?

codeexploiter
03-08-2007, 06:20 AM
Some problems in your code mentioned above which I colored in red


<!-- HIDE FROM INCOMPATIBLE BROWSERS
function Jumble()
{
var input = document.forms[0].description.value;
var split = input.split(", ");
var output = "";
for(i=0; i < split.length; i++;)
{
if( i != 1 - split.length)
output = output + "-" split[i] + "<br />\n\n";
else
output = output + "-" split[i] + "<br />";
}
document.forms[0].description.value = output;
}

// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</script>
</head>

<body>

<form >
<p>Description Formater<br />
<textarea name="description" cols="50" rows="30" /></textarea> </p>
<p><input type="button" value="Format" onclick="return Jumble();" /> </p>
</form>

1.
if(i != 1 - split.length)
The condition seems to be illogical from your scripts point of view. Is it some thing like the following

if(i != (split.length - 1))

2.
output = output + "-" split[i] + "<br />\n\n";
This must be the following

output = output + "-"+split[i] + "<br />\n\n";

3.
output = output + "-" split[i] + "<br />";
This must be the following

output = output + "-"+split[i] + "<br />\n\n";

4.
for(i=0; i < split.length; i++;)
This must be the following

for(i=0; i < split.length; i++)

I would suggest not using built-in method names as your variable.