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...
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.
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.
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(" ");
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.
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.
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.
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"; }
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With