php_b34st Posted August 23, 2008 Share Posted August 23, 2008 I have the following code: <script type="text/javascript"> var str="code name club price"; document.write(str.split(" ",1) + "<br />"); document.write(str.split(" ",2) + "<br />"); document.write(str.split(" ",3) + "<br />"); document.write(str.split(" ",4)); </script> Its not quite how i wanted it to output so I was wondering how i would put it into an array instead,at the moment it outputs like this: code code,name code,name,club code,name,club,price when I only wanted: code name club price is there a way to put them in an array so that i can use each element seperately? Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 23, 2008 Share Posted August 23, 2008 BNo need to split into an array if all you want to do is replace the space characters with a line break: var str="code name club price"; document.write(str.replace(/ /g, '<br />')); Quote Link to comment Share on other sites More sharing options...
php_b34st Posted August 24, 2008 Author Share Posted August 24, 2008 I do not need to add a line break I just showed that as an example, I need to be able to use each element seperately so that I can use the data in other functions which is why i thought an array would be suitable? There will be 11 different elements for code name club price I need to display these and add all the price elements to make a total price also i need to make sure the same club has not been used more than twice and the same name has not been used more than once. I have made the php script which handles this I just need javascript to pass it onto the php side. thanks again for any help Quote Link to comment Share on other sites More sharing options...
Mijii Posted August 25, 2008 Share Posted August 25, 2008 Hi ya, <script type="text/javascript"> var tmp = str.split(" "); alert(tmp[0]); alert(tmp[1]); alert(tmp[2]); alert(tmp[3]); </script> This should do it. Quote Link to comment Share on other sites More sharing options...
php_b34st Posted August 25, 2008 Author Share Posted August 25, 2008 thanks for the help Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.