Omnimaga

General Discussion => Technology and Development => Web Programming and Design => Topic started by: Munchor on March 14, 2011, 09:12:43 am

Title: Javascript Problems
Post by: Munchor on March 14, 2011, 09:12:43 am
Code: [Select]
<!DOCTYPE html>
<head>
<script language="javascript" type="text/javascript">
function addtext() {
var newtext = document.myform.inputtext.value;
document.myform.outputtext.value += newtext;
}
</script>
</head>
<body>
<form name="myform">
<input type="inputtext" size="25" />
<input type ="outputtext" size = 25" />
<p></p>
<input type="button" value="Convert" onClick="addtext();">

</form>
</body>
</html>

I have this code which is supposed to pick the text from the inputtext textbox and put it in the outputtext box. It is not working though, any ideas?
Title: Re: Javascript Problems
Post by: Jim Bauwens on March 14, 2011, 09:19:38 am
Yep, I see it:
Code: [Select]
<input type="inputtext" size="25" />
<input type ="outputtext" size = 25" />

should be

Code: [Select]
<input type="text" name="inputtext" size="25" />
<input type="text" name="outputtext" size = 25" />
Title: Re: Javascript Problems
Post by: Deep Toaster on March 14, 2011, 09:21:05 am
Try

Code: (Javascript) [Select]
function addtext() {
var newtext = document.forms['myform'].elements['inputtext'].value
document.forms['myform'].elements['outputtext'].value += newtext;
}

instead.
Title: Re: Javascript Problems
Post by: Munchor on March 14, 2011, 09:22:43 am
Yep, I see it:
Code: [Select]
<input type="inputtext" size="25" />
<input type ="outputtext" size = 25" />

should be

Code: [Select]
<input type="text" name="inputtext" size="25" />
<input type="text" name="outputtext" size = 25" />

That worked perfectly!

Try

Code: (Javascript) [Select]
function addtext() {
var newtext = document.forms['myform'].elements['inputtext'].value
document.forms['myform'].elements['outputtext'].value += newtext;
}

instead.

The problem was not the function but I'm using that one since it seems more tidy.
Title: Re: Javascript Problems
Post by: jnesselr on March 14, 2011, 11:53:28 pm
Also, shouldn't:
Code: [Select]
<input type="text" name="inputtext" size="25" />
<input type="text" name="outputtext" size = 25" />
be written as:
Code: [Select]
<input type="text" name="inputtext" size="25" />
<input type="text" name="outputtext" size="25" />
Title: Re: Javascript Problems
Post by: Stefan Bauwens on March 15, 2011, 05:17:22 am
Also, shouldn't:
Code: [Select]
<input type="text" name="inputtext" size="25" />
<input type="text" name="outputtext" size = 25" />
be written as:
Code: [Select]
<input type="text" name="inputtext" size="25" />
<input type="text" name="outputtext" size="25" />
I don't really know anything of javascript but both 'codes' look the same. ???

EDIT: Oh, I see it now. :)
Title: Re: Javascript Problems
Post by: Jonius7 on March 15, 2011, 05:25:12 am
there is a slight difference but does it matter if there are spaces in between the equals sign. i guess it is better coding to have them consistent, either have spaces or don't have spaces.

<input type="text" name="outputtext" size = 25" />
Title: Re: Javascript Problems
Post by: Jim Bauwens on March 15, 2011, 05:35:15 am
I see it, its missing a <b>"</b>
Title: Re: Javascript Problems
Post by: Deep Toaster on March 15, 2011, 09:16:57 am
there is a slight difference but does it matter if there are spaces in between the equals sign. i guess it is better coding to have them consistent, either have spaces or don't have spaces.

<input type="text" name="outputtext" size = 25" />

Does HTML even accept spaces around equal signs? I thought it didn't.
Title: Re: Javascript Problems
Post by: Jim Bauwens on March 15, 2011, 09:42:44 am
Chromium doesn't seem to have a problem with it.
I guess its better to do it without spaces, for more compatibility.
Title: Re: Javascript Problems
Post by: Munchor on April 16, 2011, 09:19:02 am
I have a new question.

How to remove all spaces and lines breaks from a string using regular expressions? Thanks.
Title: Re: Javascript Problems
Post by: Deep Toaster on April 16, 2011, 10:46:32 am
YOUR_STRING_HERE.replace(/\s/g, '');

/ denotes the start (and end) of a regex expression, \s matches all whitespace, and /g ends the regex and tells it to replace every match (instead of just the first one).
Title: Re: Javascript Problems
Post by: Munchor on April 16, 2011, 10:55:28 am
YOUR_STRING_HERE.replace(/\s/g, '');

/ denotes the start (and end) of a regex expression, \s matches all whitespace, and /g ends the regex and tells it to replace every match (instead of just the first one).

What about spaces?

I have this code:

Code: [Select]
inputText = inputText.replace(/[^A-Fa-f0-9]+/g,'').replace(/(\r\n|\n|\r)/gm,"").toUpperCase();
To remove all non-hex characters and linebreaks, does it include spaces? Cos I doubt not.
Title: Re: Javascript Problems
Post by: Deep Toaster on April 16, 2011, 11:02:54 am
\s captures all linebreaks, tabs, spaces, etc. It's the same as [\r\n \t ... ].

You don't need to do \r\n|\n|\r. That's the same as [\r\n], which is included in \s.
Title: Re: Javascript Problems
Post by: Munchor on April 17, 2011, 05:40:09 am
\s captures all linebreaks, tabs, spaces, etc. It's the same as [\r\n \t ... ].

You don't need to do \r\n|\n|\r. That's the same as [\r\n], which is included in \s.

I got there, I had a return statement before crashing it all.

The online Z80 Disassembler works well thanks to that.
Title: Re: Javascript Problems
Post by: ZippyDee on April 17, 2011, 05:41:49 am
You can do [\r\n]+ to capture one or more linebreak characters in a row.
Title: Re: Javascript Problems
Post by: Munchor on April 17, 2011, 05:43:00 am
Code: [Select]
inputText = inputText.replace(/[^A-Fa-f0-9]+/g,'').replace(/\s/g,'').replace(/\n|\r|\s/g,'').toUpperCase()
That was my original code yeah, but I have shortened it now.
Title: Re: Javascript Problems
Post by: Jim Bauwens on April 17, 2011, 02:13:08 pm
\s captures all linebreaks, tabs, spaces, etc. It's the same as [\r\n \t ... ].

I didn't know that! I just thought it represented a space.
Thanks for the info :)
Title: Re: Javascript Problems
Post by: Deep Toaster on April 18, 2011, 09:23:39 am
Whitespace includes anything that's blank and a space :D