Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Help with AJAX + input fields / event is not defined.

    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?

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #3
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Scriptdaemon; 12-10-2006 at 05:47 AM.

  • #4
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.
    Last edited by Scriptdaemon; 12-10-2006 at 05:55 AM.

  • #6
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Are you using FireFox or IE?

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Firefox.

  • #8
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Change the line in testing() to

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

    and add the function

    Code:
    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

  • #9
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?

  • #10
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
    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

  • #11
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?

  • #12
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

    Code:
    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

  • #13
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?

  • #14
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #15
    New Coder
    Join Date
    Dec 2006
    Posts
    95
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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?
    Last edited by Scriptdaemon; 12-10-2006 at 06:55 AM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •