javascript doesnt continue even though it should - mysterious -


i have written simple script goes through textarea , transports !hash tags! , @person names@ 2 separate inputs.

however, unless first of 2 regex conditions met (word starts '!' , ends '!' symbol), not work next condition.

for example code on line 42 console.log(persons_parsed); doesnt execute @ all, unless !hash tag! typed textarea.

how make work, enter @person name@ , fill in input#persons_jquery ?

code in jsfiddle: http://jsfiddle.net/yz2jw/1/

the problematic part

if (tags_parsed.length > 0) {          var vysledek_tags = null;         (i = 0; < tags_parsed.length; i++) {             if (vysledek_tags) {                 var vysledek_tags = vysledek_tags + ', ' + tags_parsed[i].replace(/[!]/g, '');             } else {                 var vysledek_tags = tags_parsed[i].replace(/[!]/g, '');             }         }         $('#tags_jquery').val(vysledek_tags);      }     console.log(persons_parsed); // doesnt return value!!!      if (persons_parsed.length > 0) {          var vysledek = null;         (i = 0; < persons_parsed.length; i++) {             if (vysledek) {                 var vysledek = vysledek + ', ' + persons_parsed[i].replace(/[@]/g, '');             } else {                 var vysledek = persons_parsed[i].replace(/[@]/g, '');             }         }         $('#persons_jquery').val(vysledek); 

thank you, jakub

there substantial clean-up can done on code.

the reason mysterious "works when add second !" running code on keyup, , not when page loaded. running on start fix that.

you add string prototype on every keyup event! that's bad idea. , simple case, there no reason add string prototype @ all, unless you're going reuse in whole lot of places.

you don't cache jquery selectors, running them again on every keyup event. finally, handling of output more complex needs be. might want array.prototype.join

a fork of fiddle fixes these issues @ http://jsfiddle.net/crosseye/6sr9b/

this relevant code:

$(document).ready(function () {     var personpattern = /[@]+[\u00bf-\u1fff\u2c00-\ud7ff\w| ]+[@]/g,         tagpattern = /[\!]+[\u00bf-\u1fff\u2c00-\ud7ff\w| ]+[\!]/g,         $person = $('#persons_jquery'),         $tag = $('#tags_jquery');     var processform = function () {         var text = $('textarea.new-memory').val(),             tags_parsed = text.match(tagpattern),             persons_parsed = text.match(personpattern);         $tag.val((tags_parsed || []).join(', ').replace(/[!]/g, ''));         $person.val((persons_parsed || []).join(', ').replace(/[@]/g, ''));     };     $('textarea.new-memory').keyup(processform);     processform(); }); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -