javascript - Remove NaN when removing text from a linked input field -
i've been looking answer looking @ other posts couldn't find directly usable script (yet might ignorance toward javascript...)
i'm using script transliterate onblur foreign language's script latin alphabet :
<script> function separation() { var str = document.getelementbyid().value; var res1 = str.charat(0); var res2 = str.charat(1); var res3 = str.charat(2); document.getelementbyid().innerhtml = res1+res2+res3; } var syllable_1 = { '김' : 'kim ', '이' : 'lee ', '야' : 'ya', } var syllable_2 = { '김' : 'gim', '이' : 'i', '야' : 'ya', } function hangul_to_roman(hangul) { return syllable_1[hangul.charat(0)] + syllable_2[hangul.charat(1)] + ( hangul.length >= 3 ? "-" + syllable_2[hangul.charat(2)] : "" ); } </script> <input type="text" id="ttt" value="김김이" onblur="document.getelementbyid('demo').value = hangul_to_roman(document.getelementbyid('ttt').value)" style="text-transform: capitalize"> <input type="text" id="demo" readonly>
here have same script in fiddle : http://jsfiddle.net/lgraq/6/
the first input field contains what's supposed transliterated , second shows transliteration; prints nan when removing what's inside first input field , i'd know how remove it.
is there know how proceed ? thank !
when hangul.charat(…)
not contained in syllable
map, property access yield undefined
. adding 2 undefined
s make nan
. can prevent showing using empty string default value lookup:
function hangul_to_roman(hangul) { return (syllable_1[hangul.charat(0)] || "") + (syllable_2[hangul.charat(1)] || "") + (hangul.length >= 3 ? "-" + (syllable_2[hangul.charat(2)] || "") : ""); }
Comments
Post a Comment