Results 1 to 2 of 2

Thread: Simple Text to HTML formater

  1. #1
    Join Date
    Mar 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Simple Text to HTML formater

    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.

    Code:
    	<!-- 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?

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Some problems in your code mentioned above which I colored in red

    Code:
    <!-- 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.
    Code:
    if(i != 1 - split.length)
    The condition seems to be illogical from your scripts point of view. Is it some thing like the following
    Code:
    if(i != (split.length - 1))
    2.
    Code:
    output = output + "-" split[i] + "<br />\n\n";
    This must be the following
    Code:
    output = output + "-"+split[i] + "<br />\n\n";
    3.
    Code:
    output = output + "-" split[i] + "<br />";
    This must be the following
    Code:
    output = output + "-"+split[i] + "<br />\n\n";
    4.
    Code:
    for(i=0; i < split.length; i++;)
    This must be the following
    Code:
    for(i=0; i < split.length; i++)
    I would suggest not using built-in method names as your variable.
    Last edited by codeexploiter; 03-08-2007 at 06:25 AM. Reason: additons

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •