shawngoldw

I have tried as many different methods as I have found but I can not seem to get this working. I have a text input box called name and I have tried onChange="function" in the input declaration, I've tried name.onChange = function, attacheventlistener and attachevent. I can not seem to get this working. The closest I have came is the function running right when I start the page but never running again. Any help would be appreciated.


Re: Sidebar Gadget Development onChange event not working

ToddOs

Try "onchange" instead. Javascript events generally name everything in full lower-case, rather than camel-casing like you would expect.



Re: Sidebar Gadget Development onChange event not working

Jonathan Abbott

Check you're not getting an error in the code, the function running once and never again is a classic symptom on an exception occuring.




Re: Sidebar Gadget Development onChange event not working

shawngoldw

well i tried onchange, but its not working. It is only checking for the change when I run another function. If I change my textbox and then run another function it runs but if I don't change the text box then run another function it doesn't. Do I need some kind of event listener or something




Re: Sidebar Gadget Development onChange event not working

Jonathan Abbott

You're not suffering from the problem described in this thread are you Namely, the onchange event doesn't fire when closing the settings page.

Post the code up, if you're still having problems.




Re: Sidebar Gadget Development onChange event not working

shawngoldw

That's not really my problem. All I'm trying to do is validate real time as the user types. It really shouldn't be hard but I'm fairly new to javascript. Here is my code:

Code Snippet

function validateThis()
{
name=document.getElementById("name").value;
content.innerHTML=name;
}


Then in another function I have:
Code Snippet

schedule=schedule+"<table><tr><td>Name:</td><td><input type=\"text\" id=\"name\" onchange=\"validateThis()\"></td></tr>";


function validateThis() does not run as I type in my text box. But it checks for a change whenever I run another function. My wording may be confusing so I'll try to explain it another way.

If I do type something in my name textbox nothing will happen. But after typing it when I run someFunction() then it checks for a change in the name textbox and runs validateThis().

And if I do not type in the name textbox, then run someFuntion(), it sees there has been no change and does nothing.

someFunction() can be ANY function in my script.

It seems like it just is not checking for the change which makes me think I need some sort of event listener but I was unable to figure it out.




Re: Sidebar Gadget Development onChange event not working

Andy E

I've read this thread a few times and thought this might be the issue, but because I wasn't sure I didn't mention anything. onchange doesn't validate as you type. It fires when the object loses focus.

What you need is onkeydown with a timer to fire a function (since onkeydown fires before the key string is sent to the input box).

Andy






Re: Sidebar Gadget Development onChange event not working

shawngoldw

Thanks Andy, that solved m problem. I used onkeyup though. That seems to be working properly. Thanks for the help.




Re: Sidebar Gadget Development onChange event not working

Jonathan Abbott

Or, fire the onchange event when a key is pressed. eg.

Code Snippet

<input ... onkeypress="javascript:this.fireEvent('onchange')" ...>






Re: Sidebar Gadget Development onChange event not working

Andy E

remember onkeyup only fires when you lift your finger off the key, so I could hold the L key and type llllllllllllllllllllllllllllllllllll before the event is fired. Jonathan's right, you should use "onkeypress" (easier to cancel input then).


Andy






Re: Sidebar Gadget Development onChange event not working

shawngoldw

Thanks, but that won't be an issue in this case. I'm not validating name like my sample showed. I'm validating time and there is a maximum of two chars in my input box. onkeyup checks that neither of the two chars are letters and if they are it removes them. If there is still reason to use onkeypress please let me know because I would like my gadget to run as smoothly as possible.




Re: Sidebar Gadget Development onChange event not working

Andy E

Removing the character after it appears is old school Smile The much cleaner, more professional looking method is to block the character before the user sees it. Something like this should work:

<input ... onkeypress="event.returnValue = /\d/.test(String.fromCharCode(event.keyCode));" ... >


Andy






Re: Sidebar Gadget Development onChange event not working

shawngoldw

I wanted to block it first but I ended up settling for after, as it was simpler, but I would like to block it if possible. Do you think you could explain that code a bit for me

if event.returnValue comes back false, the key wont be put in, but if it comes back true then it will, right I have no idea what /\d/.test() does. I would put the keycodes of the characters that are acceptable in String.fromCharCode() as event.keyCode

Cheers,
Shawn




Re: Sidebar Gadget Development onChange event not working

ToddOs

event.returnValue determines whether or not the event succeeds or fails. Setting that to false will cancel the event, and thus the keypress. /\d/ is a regular expression that looks for anything that is a digit. .test() is a method on regular expressions that will apply the regular expression and return true if it matches and false if it doesn't. In this case what's happening is you apply the regular expression that checks for digits, and it will return false if there's anything but digits in the input.

Read up on regular expressions for more info.




Re: Sidebar Gadget Development onChange event not working

shawngoldw

Thanks for the explanationon. This is the code I'm using in my input declaration.

keypress=\"event.returnValue = /\d/.test(String.fromCharCode(event.keyCode));\"

It is almost working properly. It is only axxepting "d" not digits. Any ideas