Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS - Splitting a string and looping through results

Tags:

javascript

In JS, I'm having trouble working out how to split a string coming from an AJAX call.

This is what I have so far:

xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { feedUpdateResponse = xmlhttp.responseText; /////...split script...///// } } xmlhttp.open("GET","https://myDomain.com/myScript.aspx",true); xmlhttp.send(); 

Where you have /////...split script...///// in my script above, I need to add a little function that splits the string returned from my AJAX call.

The string simply contains names of DIVs, like this:

feedUpdateResponse = "div1/div2/div3/div4" 

I would like to first split the string by its slashes (/) and run a loop through the different values and do stuff to those elements on my page.

To give an idea of what I need to achieve, I have given this example which is a mix of ASP & JS - it's the only way I can possibly describe it (and show that I've had an attempt) :)

MyArray = Split(feedUpdateResponse,"/") For Each X In MyArray documentGetElementById('updateAvailable_'+x).style.visibility="visible"; Next 

On my page I have an ASP script that produces jquery carousels, all contained by separate DIVs. The DIVs are named DIV1, DIV2 etc. Inside DIV1, for example, is a text element called updateAvailable_div1 which will alert the user "There are new photos available for this feed, please click the refresh button".

Could somebody please explain to me how I can change my example above to work in JS? Just need to split the string into an array and loop through the split values...

like image 969
TheCarver Avatar asked Dec 06 '11 01:12

TheCarver


People also ask

How can I split a string into two JavaScript?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.

Does split return a string?

Using split()When the string is empty and no separator is specified, split() returns an array containing one empty string, rather than an empty array. If the string and separator are both empty strings, an empty array is returned.

How do you split a string in JavaScript?

JavaScript String split () Method. JavaScript String. split () Method. ❮ Previous JavaScript String Reference Next ❯. Example. Split a string into an array of substrings: var str = "How are you doing today?"; var res = str.split(" ");

What happens when you split a string into multiple strings?

After splitting the string into multiple substrings, the split () method puts them in an array and returns it. It doesn't make any modifications to the original string.

How to split a string into an array of substrings?

The split () method splits a string into an array of substrings. The split () method returns the new array. The split () method does not change the original string. If (" ") is used as separator, the string is split between words. Optional.

How do you loop through a string in JavaScript?

So, how exactly do you loop through a string in JavaScript? Let’s say we have a string like so: const text = new String ( "Tommy") Using the latest in ES6 syntax: [...text] .for Each (char => console.log(char)) Notice how we are spreading text into an array and then using forEach as you would for any other array type.


2 Answers

You can use .split() to split a string on a specified character with the results returned as an array. So then it's just a matter of looping through the array:

// given your existing variable // feedUpdateResponse = "div1/div2/div3/div4" as set in the // code in the question, add this:  var a = feedUpdateResponse.split("/"),     i;  for (i = 0; i < a.length; i++) {     document.getElementById("updateAvailable_" + a[i]).style.visibility                                                                  = "visible"; } 
like image 200
nnnnnn Avatar answered Oct 13 '22 21:10

nnnnnn


Get your array via string.split("/"). Iterate your array using your method of choice. I prefer Array.forEach():

feedUpdateResponse.split("/").forEach(function (item) {     document.getElementById(item).style.visibility = "visible"; }); 

See the compatibility notes for using .forEach() in older browsers.

like image 29
gilly3 Avatar answered Oct 13 '22 21:10

gilly3