Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - JavaScript onclick help. | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
SyntaxLegend

Double metal axe
Level: 25

Posts: 202/222
EXP: 78264
For next: 11356

Since: 04-21-04
From: Australia

Since last post: 20 days
Last activity: 10 hours
Posted on 07-15-05 08:06 PM Link | Quote
With javascript, is there anyway to make a textbox's value dissapear when it is a certain value.
Like if the value of the textbox is "12345", only when the textbox is "12345" it sets it so there is nothing in the textbox, but if the textbox is "123456" it leaves it. Anyway to do that?
Narf
Hi Tuvai!
(reregistering while banned)
Level: 16

Posts: 71/100
EXP: 17634
For next: 2622

Since: 12-26-04

Since last post: 22 hours
Last activity: 14 hours
Posted on 07-16-05 01:05 AM Link | Quote
onclick="if(this.value=='12345'){this.value=' ';}"
BGNG

Snifit
Level: 22

Posts: 141/276
EXP: 56579
For next: 1771

Since: 06-03-05

Since last post: 8 days
Last activity: 3 hours
Posted on 07-16-05 01:06 AM Link | Quote
Yup. You can do that with a simple comparison check:

<HTMl><Body>
<Form>
<Input Type="Text" Name="textbox"><BR>
<Input Type="Button" Value="Click!"
onClick="if (this.form.textbox.value == '12345') this.form.textbox.value = '';">
</form>
</body></html>
neotransotaku

Baby Mario
戻れたら、
誰も気が付く
Level: 87

Posts: 3566/4016
EXP: 6220548
For next: 172226

Since: 03-15-04
From: Outside of Time/Space

Since last post: 11 hours
Last activity: 1 hour
Posted on 07-16-05 01:55 AM Link | Quote
you may need some whitespace character trimming because i'm sure you know <space>12345 is not the same as 12345


(edited by neotransotaku on 07-15-05 08:58 PM)
BGNG

Snifit
Level: 22

Posts: 142/276
EXP: 56579
For next: 1771

Since: 06-03-05

Since last post: 8 days
Last activity: 3 hours
Posted on 07-16-05 05:44 AM Link | Quote
Okay... I've written a function for trimming off spaces and tabs from the beginning and end of strings. This HTML file will do what you want:

<HTML><Head>

<Script Language="JavaScript">

function Trim(In) {
    var T = In; var Tab = "\u0009"; var Space = " ";
    while (T.charAt(0) == Tab || T.charAt(0) == Space)
        T = T.substring(1, T.length);
    while (T.charAt(T.length - 1) == Tab ||
           T.charAt(T.length - 1) == Space)
        T = T.substring(0, T.length - 1);
    return T;
}

</Script>

</Head><Body>

<Form>
<Input Type="Text" Name="textbox"><BR>
<Input Type="Button" Value="Click!"
onClick="if (Trim(this.form.textbox.value) == '12345') this.form.textbox.value = '';">
</Form>

</Body></HTML>
Ramsus

Octoballoon
Level: 19

Posts: 141/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 07-17-05 02:19 AM Link | Quote
<input type="text" onchange="if (parseInt(this.value) == 12345) this.value='';">

and for strings:

<input type="text" onchange="if (this.value.match(/^\s*(12345)\s*/)) this.value='';">


(edited by Ramsus on 07-16-05 05:20 PM)
(edited by Ramsus on 07-16-05 05:39 PM)
BGNG

Snifit
Level: 22

Posts: 146/276
EXP: 56579
For next: 1771

Since: 06-03-05

Since last post: 8 days
Last activity: 3 hours
Posted on 07-17-05 04:23 AM Link | Quote
The only things that are restrictive, Ramsus, is that 1) the "this.value" only works if the statement is triggered by the text input's events and 2) the string to check is the numeric value of 12345. What if we want to click a button and check for "zyxwv"?
SyntaxLegend

Double metal axe
Level: 25

Posts: 203/222
EXP: 78264
For next: 11356

Since: 04-21-04
From: Australia

Since last post: 20 days
Last activity: 10 hours
Posted on 07-17-05 07:09 AM Link | Quote
I want it to be able to do it for alphanumeric, the "12345" was just an example.
Ramsus

Octoballoon
Level: 19

Posts: 142/162
EXP: 34651
For next: 1126

Since: 01-24-05
From: United States

Since last post: 39 days
Last activity: 71 days
Posted on 07-17-05 08:17 AM Link | Quote
Originally posted by BGNG
The only things that are restrictive, Ramsus, is that 1) the "this.value" only works if the statement is triggered by the text input's events and 2) the string to check is the numeric value of 12345. What if we want to click a button and check for "zyxwv"?


1. Change this.value to a reference to the value of the field you want to check and call it when you want the value checked. It's not brain surgery, and anyone who knows Javascript can do it in 10 seconds with a decent DOM reference.

<script type="text/javascript" language="javascript">
<!--
function checkInput(form) {
if (form.exampleInput.value.match(/^\s*(12345)\s*/$))
form.exampleInput.value = '';
return false;
}
// -->
</script>

<form onsubmit="return checkInput(this);" action="" method="GET">
<input type="text" name="exampleInput">
<input type="submit">
</form>

From the original post though, it sounded as though he wanted to check the input after the user changed it, not after pushing a button.

2. Did you even look at the second example?

<input type="text" onchange="if (this.value.match(/^\s*(zyxwv)\s*/$)) this.value='';">

Javascript has had regular expressions for a long time. Works in Mozilla, Safari, and IE mac, and is well documented as working in IE4 by MSDN.

EDIT: As a form with a button:

<script type="text/javascript" language="javascript">
<!--
function checkInput(form) {
if (form.exampleInput.value.match(/^\s*(zyxwv)\s*/$))
form.exampleInput.value = '';
return false;
}
// -->
</script>

<form onsubmit="return checkInput(this);" action="" method="GET">
<input type="text" name="exampleInput">
<input type="submit">
</form>

This also has the added benefit of simply running checkInput() on the form if the user hits enter. If you want to actually submit the form, change checkInput so it returns true. It'll check the input with checkInput, and then submit the form.


(edited by Ramsus on 07-16-05 11:18 PM)
(edited by Ramsus on 07-16-05 11:33 PM)
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
Acmlm's Board - I2 Archive - Programming - JavaScript onclick help. | |


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.009 seconds.