Marco Marco - 6 months ago 11
Javascript Question

Selecting element with a space separeted words

I am adding elements in my view dynamically, like this:

@foreach ($categoryViews as $key => $value)
<div class="large-2 columns text-center">
<div id='{{ $key }}' style="height: 101px; width: 102px; margin: 0 auto"></div>
</div>
@endforeach


But one of my keys is a string that is space separated, it looks like this
Product test


I know ids must not be separated, but I am trying to find a way to select somehow those elements, beacause I will be getting them from my DB, so instead of going through changes of making slugs for them, I was wondering how would I achieve that with jQuery, and I thought it was possible to do it since there was already a topic about it on stack overflow. In my script I am trying to initiate charts for each element, by going through an the same array. I am using
$('[id=' + key + ']')
as a selector for space separated words by following this example, like this:

for (var key in icoop.viewsByCategory) {
$('[id=' + key + ']').highcharts({
// my code...
}


In my console I get an error:


jquery.js:1468Uncaught Error: Syntax error, unrecognized expression: [id=Product test]

Answer

Identifier can't contain space, However still you need to use it then, the Attribute Equals Selector [name=”value”] can be used.

$('[id="' + key + '"]').highcharts()
Comments