javascript - How to get "this" variable mixing php and jQuery?


I have a set of boxes:


<button >
  <div>Ajax content 1</div>
<button >
  <div>Ajax content 1</div>

In the PHP loop I do:

$permalink = get_permalink(); // Link of the box
var simple = '<?php echo $permalink; ?>';

The php loop above prints the correct link for each box link in eachsimple variable

I then call the content via ajax like this:

$(document).on( 'click', ".btn-modal", function(){
  var cont = $(this).simple +  " .content"; // Load via ajax the Box link + content 

The variablesimple is applied to all the boxes correctly but I am not gettingthis box link on click but I am getting a no found error instead 404 (Not Found)



I guess i understand what you want to do. Your approach is not the correct way, you want to save a link for each div and then use that link in some JS logic.

Then in your loop (this is Wordpress right ?) you need to set out the link in the DOM element you want inside adata-attr and also give some class name to that div so that you can actually select it using JS

echo '<div class="someClass" data-src='. get_permalink() . '> ..... 

The result should be:

<div class="someClass" data-src="http://..." >Ajax content 1</div>

Now, your JS

$(document).on( 'click', ".btn-modal", function(){
  var cont = $(this).children('.someClass').attr('data-src') + " .content";

People are also looking for solutions to the problem: mysql php check form for empty input fields


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.