1

Hi I am trying to display some elements using querySelectorAll to capture attributes and DOM the css for the elements, till now faced with "Syntax error: An invalid or illegal string was specified" will appreciate some help here.

Here is my code:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>select sort function</title>
<style>
a{text-decoration:none;display:none;font-family:sans-serif,arial;}
</style>
</head>
<body>
<select id="sortsel" onchange="listFunction()">
    <option value='0'>- All Sorting -</option>
    <option value='1'>Sort A</option>
    <option value='2'>Sort B</option>
</select><br/><br/>
<div>
<a href="javascript:void(0)" data-sortid="1">Sort A sub 1</a><br/>
<a href="javascript:void(0)" data-sortid="1">Sort A sub 2</a><br/>
<a href="javascript:void(0)" data-sortid="1">Sort A sub 3</a><br/>
<a href="javascript:void(0)" data-sortid="2">Sort B sub 1</a><br/>
<a href="javascript:void(0)" data-sortid="2">Sort B sub 2</a><br/>
<a href="javascript:void(0)" data-sortid="2">Sort B sub 3</a><br/>
</div>
<script>
function listFunction(){
    var selectSort = document.getElementById("sortsel");
    var selectedSort = selectSort.options[selectSort.selectedIndex].value;
    console.log(selectedSort);
    var testing = '[';
    testing += 'data-sortid="';
    testing += selectedSort;
    testing += '"]';
    console.log(testing);
    document.querySelectorAll(testing).style.display="block";
}
</script>
</body>
</html>

Much appreciate any help here.

4 Answers 4

2

if we want to query node attribute we may use [data-sortid='1'] like structure

Javascript

function listFunction(){
    var selectSort = document.getElementById("sortsel");
    var selectedSort = selectSort.options[selectSort.selectedIndex].value;
    console.log(selectedSort);
    var testing = '';
    testing += '[data-sortid="';
    testing += selectedSort;
    testing += '"]';
    console.log(testing);

    [].forEach.call(document.querySelectorAll(testing), function (el) {
        el.style.display="block";
    });
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Sanjay. I didn't realise the DOM was put in as an ARRAY.
You can further optimize the code... see answer stackoverflow.com/a/38279259/3391120
1

Code Optimization from the sanjay's answer. https://stackoverflow.com/a/38198918/3391120

Run the snippet to see the results. It'll reset the sort result if you click on first item from the select box.

function listFunction(){
    var selectSort = document.getElementById("sortsel");
    var selectedSort = selectSort.options[selectSort.selectedIndex].value;
  var sortResultItems =  document.querySelectorAll("[data-sortid]");
  globalVar = sortResultItems;

sortResultItems.forEach(function(item, index) {
  item.style.display = "none";
  if((parseInt(selectedSort) !== 0) && parseInt(item.dataset.sortid) === parseInt(selectedSort)){
    item.style.display = "block";
  }
});
}
a{text-decoration:none;font-family:sans-serif,arial;}
.sort-result-item {
  display:none;
}
<select id="sortsel" onchange="listFunction()">
    <option value="0">- All Sorting -</option>
    <option value="1">Sort A</option>
    <option value="2">Sort B</option>
</select><br/><br/>
<div>
  <div id="sortResults">
    <div data-sortid="1" class="sort-result-item">
      <a href="javascript:void(0)">Sort A sub 1</a><br/>
      <a href="javascript:void(0)">Sort A sub 2</a><br/>
      <a href="javascript:void(0)">Sort A sub 3</a><br/>
    </div>
    <div data-sortid="2" class="sort-result-item">
      <a href="javascript:void(0)">Sort B sub 1</a><br/>
      <a href="javascript:void(0)">Sort B sub 2</a><br/>
      <a href="javascript:void(0)">Sort B sub 3</a><br/>
    </div>
  </div>


</div>

1 Comment

yes @rahul foreach code is used in may ways in javascirpt.
0

Your query has to look like [data-sortid="1234"]. You are missing the surrounding brackets [..].

Try this:

var testing = '[';
testing += 'data-sortid="';
testing += selectedSort;
testing += '"]';

3 Comments

changed the query to accomodate the [..], error still persists. =(
What's the output of console.log(testing)?
it output [data-sortid="1"] sanjay helped solved it. I didn't realise the query was put in as an array.
0

The problem is that document.querySelectorAll(testing) returns an array of elements. You can iterate through them and change their style.diplay like this:

  var arguments = document.querySelectorAll(testing);
  for (var i = 0; i < arguments.length; i++) {
    arguments[i].style.display = "block";
  }

See demo http://codepen.io/8odoros/pen/ZOyoBN

Comments

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.