0

I have Wordpress site http://gambit.co/test with Visual Composer plugin which allows me to create pages in WYSIWYG mode. All content created by it is loaded with ajax and javascript. I have some nice media grid section but i cant assign specific links to squares. They all are minatures connected to their images.

I tried to replace their links with jQuery

jQuery(document).ready( function($) {
$("a[href='http://gambit.co/test/wp-content/uploads/2016/07/600_wynajem.jpg']").attr('href', 'http://www.google.com/');

});

But i doesnt work as well because there is no HTML content when scripts runs. I moved the script to the bottom of the page, just before closing BODY tag but id didnt work. I tried both with .attr and .prop. What should i do?

Dominik
  • 21
  • 2
  • `content is loaded with ajax and javascript;` so does it mean that its getting loaded by code you written using `$.ajax`; Or its something else? – vijayP Jul 08 '16 at 09:43

1 Answers1

0

Try to take a look to DOMNodeInserted.

In this way you can write something like:

// as soon as a new anchor tag is added to the dom and
// the href value of this element is......
$(document).on('DOMNodeInserted', 'a[href="http://gambit.co/test/wp-content/uploads/2016/07/600_wynajem.jpg"]', function(e) {
  this.href = 'http://www.google.com/';
  
  this.textContent = 'http://www.google.com/';
});


$(function () {
  $('#btn').on('click', function(e) {
    $('body').append('<a href="http://gambit.co/test/wp-content/uploads/2016/07/600_wynajem.jpg">My sample</a>');
  })
});
<script src="https://code.jquery.com/jquery-1.12.1.min.js"></script>

<button id="btn">Add link</button>
gaetanoM
  • 41,594
  • 6
  • 42
  • 61