- Forums
- Javascipt
- Create Array From Variable In Javascript As Explode() In Php
this page will show you with sample code on how you can create an array from a variable string like using php explode() function to create arrays but in javascript code [836], Last Updated: Sat May 18, 2024
wallpaperama
Sun Oct 23, 2011
0 Comments
3468 Visits
in this example i will show you how you can use the javascript function called split() similar to PHP function explode() to create an array from a string
here is the code:
this code lets the user enter a color and it finds a match within the array. it uses the split() function in javascript. similar to php explode() function: <hr />
<script type="text/javascript" language="javascript">
function Search(id){
var Mycolor = document.getElementById(id).value
for (i=0;i<Counts;i++){
if(ColorsArray[i] == Mycolor){
alert('You Entered: ' + Mycolor +' and I found ' + ColorsArray[i] + ' on element #' + i);
return;
}
}
alert('I didnt find ' + Mycolor + ' In the Array');
}
var Colors = 'red|blue|white|green';
var ColorsArray = Colors.split("|");
var Counts = ColorsArray.length;
document.write('Colors Available: ');
for (i=0;i<Counts;i++){
document.write(ColorsArray[i] + ', ');
}
</script>
<br /><br />
Enter Color to find: <input type="text" id="MyColor" /><input type="button" value="Search" onclick="Search('MyColor');" />
<div style="text-align:center;padding:15px;">By <a href="http://www.wallpaperama.com">Wallpaperama.com</a><!-- please dont remove our link--></div>