2 Replies - 417 Views - Last Post: 17 April 2011 - 08:10 AM

#1 Faeleth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 11

Help with conversion

Posted 16 April 2011 - 10:14 PM

Hi I'm having trouble (with an assignment) converting ampersands to "and". I'm out of ideas on how to get this functional. And tips or direction would be greatly appreciated.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">


<head>
<title>Chapt11_1</title>

<script type = "text/javascript">

<!--
	   var myString = document.getElementById("formText").value;
   	   var newString = "and"; 
	
	function convertit()
	{
               for(var i = 0; i< myString.length;i++)
 		{
		 
                 if(myString.charAt(i) == "&")
         
		 newString += "and";

                  else
                 newString +=myString.charAt(i);
			
	     	}
       }
// -->
</script>
</head>
<body>
<form>
<input type="text" id="formText" onblur="convertit()" />
</form>
</body>
</html>





Is This A Good Question/Topic? 0
  • +

Replies To: Help with conversion

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3047
  • View blog
  • Posts: 4,562
  • Joined: 08-June 10

Re: Help with conversion

Posted 17 April 2011 - 12:44 AM

Hey.

Firstly, you're probably going to want to put the myString and newString variables inside the function, or they will just be set once when the page loads, which means the myString variable will always be the initial value of your formText input, which is presumably empty.

That said, you may want to check out the String.replace function.
Was This Post Helpful? 0
  • +
  • -

#3 Faeleth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 23-February 11

Re: Help with conversion

Posted 17 April 2011 - 08:10 AM

View PostAtli, on 17 April 2011 - 12:44 AM, said:

Hey.

Firstly, you're probably going to want to put the myString and newString variables inside the function, or they will just be set once when the page loads, which means the myString variable will always be the initial value of your formText input, which is presumably empty.

That said, you may want to check out the String.replace function.



Thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1