Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
How to pass value in key value pair jquery?

I want to pass(return) data-filter value and children text , I am able to pass the data-filter value but I am unable to pass the children text. My HTML and JQuery as following:

$(document).ready(function() {
  $('.sidebar-filter').on('click', function() {
    var filterobj = {};
    $(".sidebar-filter").each(function(index, ele) {

      var filterval = $(this).children('a').text();
      var filterkey = $(this).data('filter');

      filterobj[filterkey] = Array.from(document.querySelectorAll('li[data-filter=' + filterkey + '].active')).map(function(el) {
        return ele.value;
      });

    });
    console.log(filterobj);

  });
});
<ul>
  <li class="sidebar-filter " data-filter="category" data-value="1">
    <a href="#">Item Name</a>
  </li>
</ul>

My return will be like:

category: Array [ undefined ]

I want value inside the array instead of undefined.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Your lis don't have attribute value, actually you want to read attribute data-value, you can achieve your goal by converting return ele.value; to return el.getAttribute('data-value');

$(document).ready(function() {
  $('.sidebar-filter').on('click', function() {
    var filterobj = {};
    $(".sidebar-filter").each(function(index, ele) {

      var filterval = $(this).children('a').text();
      var filterkey = $(this).data('filter');

      filterobj[filterkey] = Array.from(document.querySelectorAll('li[data-filter=' + filterkey + '].active')).map(function(el) {
        return el.getAttribute("data-value");
      });

    });
    console.log(filterobj);

  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<ul>
  <li class="sidebar-filter active" data-filter="category" data-value="1">
    <a href="#">Item Name</a>
  </li>
</ul>

about 4 years ago · Juan Pablo Isaza Report

0

Posting answer, might be of help to someone others too

    filterobj[filterkey]= Array.from(document.querySelectorAll
          ('li[data-filter=' + filterkey+'].active')).map(function(el){
            return $(el).data("value")
 });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!