My requirement is to split some text to array, lets say
"blabla32020|dmakdj9230|3023902|dkasdfj39|etcetc"
And I will also have div's with same ids example:
<div id=blabla32020></div>
I want to change the background color of those to one color. And also to ignore if one element is not there (example if there is no div for dkasdfj39, then I should not get an error). I tried this but it gives me a Type error (showing an entermark in front of blabla32020) on Chrome.
var bdata = bookcaldates.innerHTML.replace(/-/g,"");
var bookdatesreference = bdata.split("|");
for(var i=0; i<bookdatesreference.length; i++)
{bookdatesreference[i].style.color='white';}
asked Aug 31, 2013 at 17:49
Gomida Matheesha Jayasinghe
1771 silver badge9 bronze badges
4 Answers 4
Using multiple selector:
$('#' + stringToSplit.replace(/\|/g, ',#')).css('color','white');
answered Aug 31, 2013 at 17:58
moonwave99
22.9k3 gold badges49 silver badges71 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Gomida Matheesha Jayasinghe
I get the following error on google chrome: Uncaught Syntax error, unrecognized expression: # 31082013PV1 Note: 31082013PV1 is the div ID. Btw somehow there's a space between # and 3
Gomida Matheesha Jayasinghe
Did with removing tab characters. bookcaldates.innerHTML = bookcaldates.innerHTML.replace(/-/g,""); bookcaldates.innerHTML = bookcaldates.innerHTML.replace(/\|/g,",#"); bookcaldates.innerHTML = "#"+bookcaldates.innerHTML; bookcaldates.innerHTML = bookcaldates.innerHTML.replace(/(\r\n|\n|\r)/gm,""); bookcaldates.innerHTML = bookcaldates.innerHTML.slice(0, -2); jQuery.noConflict(); $(bookcaldates.innerHTML).css('color','white');
try this with jQuery:
for(var i=0; i<bookdatesreference.length; i++)
{
$('#' + bookdatesreference[i]).css('color', 'white');
}
answered Aug 31, 2013 at 17:52
Alessandro Minoccheri
36.1k22 gold badges128 silver badges176 bronze badges
1 Comment
Gomida Matheesha Jayasinghe
<exception>: "Syntax error, unrecognized expression: #↵31082013PV1" a: "#↵31082013PV1"
FINAL ANSWER:
bookcaldates.innerHTML = bookcaldates.innerHTML.replace(/-/g,"");
bookcaldates.innerHTML = bookcaldates.innerHTML.replace(/\|/g,",#");
bookcaldates.innerHTML = "#"+bookcaldates.innerHTML;
bookcaldates.innerHTML = bookcaldates.innerHTML.replace(/(\r\n|\n|\r)/gm,"");
bookcaldates.innerHTML = bookcaldates.innerHTML.slice(0, -2);
jQuery.noConflict();
$(bookcaldates.innerHTML).css('color','white');
answered Aug 31, 2013 at 20:53
Gomida Matheesha Jayasinghe
1771 silver badge9 bronze badges
Comments
If($("#" + bookdatesreference[i]))
$("#" + bookdatesreference[i]).css('color','white')
answered Aug 31, 2013 at 18:01
Richie Fredicson
2,5033 gold badges18 silver badges19 bronze badges
1 Comment
Gomida Matheesha Jayasinghe
<exception>: "Syntax error, unrecognized expression: #↵31082013PV1" a: "#↵31082013PV1"
lang-js