javascript - insertAfter() is duplicating an element when I don't want it to -


okay, i'm making chatbot, , ran problem. need make function creates chat message everytime enter key pressed. far, it's coming out nicely, 1 problem. it's duplicating element, want 1 of.

to see i'm talking about, go http://jsfiddle.net/matthewkosloski/bhxma/ , type out message, hit enter. notice how there 2 "foo!" messages? want one. i'd want make these messages go top-bottom in chronological order, can't until find out why duplication occuring!

function insertafter(referencenode, newnode) {     referencenode.parentnode.insertbefore(newnode, referencenode.nextsibling); }  var robotmessage = usermessage;  function intelresponse(){          // robot         var robot = document.createelement("h4");         var robottext = document.createtextnode("robot");         robot.appendchild(robottext);         robot.classname = "rtitle";         document.body.appendchild(robot);         insertafter(usermessage, robot);          // robot's response         robotmessage = document.createelement("span");         var robotmessagetext = document.createtextnode("foo");         robotmessage.appendchild(robotmessagetext);         robotmessage.classname = "rmsg";         document.body.appendchild(robotmessage);         insertafter(robot, robotmessage);  } 

a simple mistake . calling method intelresponse twice. fixed issuefiddle

if (e.keycode == 13) {         submitusermessage();         //intelresponse();         $("#user-input").val("");     } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -