I'm trying to use dataset content in my Javascript, but I can't get it to work. Is this even possible or has the content run too late? The values are being output in console correctly.

// JavaScript Document jQuery(document).ready(function($) { var page = 2; var post_type = 'consultant'; $(".loadmore").click(function() { const loadingcons = document.getElementById('loadingmore'); var consultantcattest = loadingcons.dataset.consultantcat; var locationtest = loadingcons.dataset.location; var locationvaltest = loadingcons.dataset.locationval; var insurertest = loadingcons.dataset.insurer; var linkontest = loadingcons.dataset.linkon; console.log(consultantcattest); console.log(locationtest); console.log(locationvaltest); console.log(insurertest); console.log(linkontest); var data = { 'action': 'codebykishor_load_more_posts', 'page': page, 'post_type': post_type, 'consultantcat': loadingcons.dataset.consultantcat, 'insurer': loadingcons.dataset.insurer, 'linkon': loadingcons.dataset.linkon, 'location': loadingcons.dataset.locationval }; $.post(loadmore_params.ajaxurl, data, function(response) { $('.post-list').append(response); page++; }); }); // Optional: Load first page automatically (Method 2 only) if ($('.post-list').is(':empty')) { $('.loadmore').click(); } }); 

2 Replies 2

Could you post this as a question?

Is it possible to use data in your code? Yes. But is that really the general advice you’re looking for, as asked?

If you are instead trying to debug specific code and solve a specific problem then you have selected the wrong question type. If there’s no option to change the question type, can you delete it and re-post it as the correct type? When doing so, please be sure to include information about the problem and what debugging you have done.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.