3

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';}

4 Answers 4

1

Using multiple selector:

$('#' + stringToSplit.replace(/\|/g, ',#')).css('color','white');
Sign up to request clarification or add additional context in comments.

2 Comments

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
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');
0

try this with jQuery:

for(var i=0; i<bookdatesreference.length; i++) 
{
   $('#' + bookdatesreference[i]).css('color', 'white');
}

1 Comment

<exception>: "Syntax error, unrecognized expression: #↵31082013PV1" a: "#↵31082013PV1"
0

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');

Comments

0
If($("#" + bookdatesreference[i]))
$("#" + bookdatesreference[i]).css('color','white')

1 Comment

<exception>: "Syntax error, unrecognized expression: #↵31082013PV1" a: "#↵31082013PV1"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.