separate values of a textbox and place it on an array in javascript -
i have textbox in user going input value want take , following. want able separate each word , add end of them. if put 123. want separate , make 1.jpg, 2,jpg 3.jpg after separated want put in array compare array , go there. here got far
<input type="text" id="textfromuser" /> <script type = "text/javascript"> function validate(){ var lists = []; var validation = document.getelementbyid("textfromuser").value; lists.push(validation); for(var i=0; i<lists.length; i++) alert(lists[i]).split('.');
this part of code show value in textbox split , placed in array not working.. ideas?
i think confusing arrays , strings, value obtain input string , you're afterwards adding lists array , iterating on array.
may looking for
html
<input type="text" id="textfromuser" />
javascript
function validate(){ // empty array var ar = []; // obtain string input "text" field, retrieved "id" attribute var validation = document.getelementbyid("textfromuser").value; // split string character array var lists = validation.split(''); // iterate on character array for(var i=0; i<lists.length; i++){ // create string concatenating array's element , '.jpg' var str = lists[i]+'.jpg'; // add string var array ar.push(str); } return ar; }
i created jsfiddle test if want try http://jsfiddle.net/kpw23/2/
update
to compare arrays there many ways accomplish it. simple way achieve serialize arrays , compare serialized strings:
// having 2 arrays var list = ['1.jpg','2.jpg','3.jpg']; var list2 = ['1.jpg','2.jpg','3.jpg']; // "serialize" arrays, join() method /// joins elements of array string, , returns string serlist = list.join(); serlist2 = list2.join(); // compare both serialized arrays if(serlist==serlist2){ alert("arrays equal"); } else{ alert("arrays not equal"); }
this method work if arrays sorted same way , contain same entries in same array positions.
Comments
Post a Comment