...

View Full Version : Help with AJAX + input fields / event is not defined.



Scriptdaemon
12-10-2006, 05:14 AM
Alright, I have two problems. First, I have created a code that will turn a link into an input field when clicked, then turned back into a link with the updated text when enter is clicked. However, when using characters like "<," ">," and "&", if I click again it gives me the htmlentity of the character instead of the character itself. How would I fix this?

http://moth.homelinux.com/~scriptdaemon/test.html

(For an example, click one of the links, type "<test>", press enter, then click again. It'll show "&lt;test&gt;" instead.)

Second, when I press the arrow keys I get the error "event is not defined," and sometimes I get a warning saying "function onkeypress does not always return a value." How would I stop the error, and is the second thing even a problem?

david_kw
12-10-2006, 05:34 AM
For the first, you have a function escapeName() but I don't see one to unEscapeName(). That is the reason your edit box shows the &lt; in place of <.

For the second, I don't think event is always defined (as I recall FF is the one but I'm not sure). You have to check and see if the event is valid and if it isn't then set it like so

if (!e) e = window.event;

something like that.

Just out of curiosity what do you consider AJAX about that page?

david_kw

Scriptdaemon
12-10-2006, 05:43 AM
For the first solution, I can't quite seem to figure out how to reverse it, because anything I tried didn't change anything.

Edit: I don't understand though, because I change it to & #60; instead of &lt;, etc. So shouldn't it show the number version instead?

Second, as for the event thing, where would I put that to not bring that error back?

Edit: Oh. and about the AJAX: I haven't actually put any of that code in, but this will be the basis of it. I'm going to have it update the database after something is entered in an input field.

david_kw
12-10-2006, 05:50 AM
For the first this is untested by maybe try

name = name.replace(/&lt;/g, "<");

etc... Basically reversing the other function. I'm not sure why the # version works actually. You might be able to reverse it the same way but this should work too.

For the second there is a line

e = event

I suspect that is supposed to be

e = window.event;

david_kw

Scriptdaemon
12-10-2006, 05:53 AM
I tried just that and it didn't do anything.


Second, if I do that then I get "reference to undefined property window.event" and "e has no properties"

Edit: This may help just a bit, but I have studied the isEnter code from another source and I get the same thing there as well, so is that just something I'll have to live with? Because what I want the code to do is actually being done.

david_kw
12-10-2006, 06:03 AM
Are you using FireFox or IE?

Scriptdaemon
12-10-2006, 06:08 AM
Firefox.

david_kw
12-10-2006, 06:10 AM
Change the line in testing() to

document.getElementById("input" + number).value = unescapeName(response);

and add the function



function unescapeName(name) {
name = name.replace(/&amp;/g,"&")
name = name.replace(/&lt;/g,"<")
name = name.replace(/&gt;/g,">")
name = name.replace(/&quot;/g,"\"")
name = name.replace(/&apos;/g,"'")
return name;
}


That worked for me in a test program.

david_kw

Scriptdaemon
12-10-2006, 06:14 AM
Oops, I forgot to change the line in the testing function. No wonder it didn't work for me before. XD.

Thanks for that, so about the other thing: is that just something that is just going to do that regardless?

david_kw
12-10-2006, 06:23 AM
Nope, that shouldn't happen. It looks like your double test

if (e && e.which)

is the problem. Because e could be valid and e.which could be 0 which would make this if statement fail.

Try this



function isEnter(e) {
var characterCode;
if (!e) e = window.event;
if(e.which) {
characterCode = e.which
} else {
characterCode = e.keyCode
}
if(characterCode == 13)
return true;
return false
}


david_kw

Scriptdaemon
12-10-2006, 06:31 AM
Wow, that worked. Thanks.

Just a smaller question, javascript "warnings" are nothing to worry about, right?

For example, "function onkeypress does not always return a value"

Edit: Two questions actually, I noticed that some lines don't have a semi-colon at the end. I thought all lines were supposed to? What's the deal on this?

david_kw
12-10-2006, 06:39 AM
First, I'd get rid of any warnings if possible. I have yet to see a warning I couldn't get rid of.

I'm not sure what the return value does for onkeypress you'd have to look in to that. But to get rid of the warning do something like this.



document.getElementById("td" + number).innerHTML = "<input type=text id=input" + number + " onkeypress=\"if(isEnter(event)){testing2(" + number + "); return false;} return false;\" />";


As for the semi-colon that is a good question. Up until a week ago I thought the only lines that could end without a semi-colon were those at the end of a code block. But I've seen a few scripts that work fine without semi-colons. Personally I always put them in when I'm writing. I have yet to see a problem with putting them in and am not sure if there are any possible problems that occur if you don't.

david_kw

Scriptdaemon
12-10-2006, 06:42 AM
That worked, but I also have an error "assignment to undeclared variable i"

I thought for statements you didn't have to declare the variable otherwise?

david_kw
12-10-2006, 06:46 AM
I don't see a for statement with i in the code you posted, but it should look something like this.

for (var i = 0; i < 10; i++)

And yes you should declare it. If you don't that value is global and could conflict with another for i loop that is nested in some other function.

david_kw

Scriptdaemon
12-10-2006, 06:51 AM
Oh, I forgot to mention I re-uploaded my script. It now has one in it. (createTable(); )

How would I declare it specifically for that function then?

Edit: I just re-read this, and I probably asked a really stupid question. The answer would be to declare it inside the function, right?

david_kw
12-10-2006, 07:01 AM
Yep, right after (or before)

var html ...

or in the for loop itself as I showed in my last post and you should be good to go.

david_kw

Scriptdaemon
12-10-2006, 04:42 PM
Sorry for the late reply, my Internet went out last night.

And I know what I did wrong now, I did exactly that for my for statement except I forgot the "var" in front.

Scriptdaemon
12-10-2006, 06:09 PM
First, I'd get rid of any warnings if possible. I have yet to see a warning I couldn't get rid of.

I'm not sure what the return value does for onkeypress you'd have to look in to that. But to get rid of the warning do something like this.



document.getElementById("td" + number).innerHTML = "<input type=text id=input" + number + " onkeypress=\"if(isEnter(event)){testing2(" + number + "); return false;} return false;\" />";


As for the semi-colon that is a good question. Up until a week ago I thought the only lines that could end without a semi-colon were those at the end of a code block. But I've seen a few scripts that work fine without semi-colons. Personally I always put them in when I'm writing. I have yet to see a problem with putting them in and am not sure if there are any possible problems that occur if you don't.

david_kw

Oh, and as for this, adding an extra return false; made it so I couldn't do anything to the input field at all, so I messed with it and took out the return false; that was already in it and that got rid of the warning.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum