0

I want to make a div clickable for the link that is contained in that div. I cannot use onclick function since that link is created dynamically. It is a wordpress website.

I tried this method,

$(".pt-cv-ifield").click(function(){
 window.location=$(this).find("a").attr("href"); 
 return false;
});

I created this jquery and saved this as clickable.js, and added it to functions.php in my theme file.

function my_theme_scripts() {
wp_enqueue_script( 'clickable', get_template_directory_uri() . '/js/clickable.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_scripts' );

Not working. Jquery is a working solution anymore. So past answers are no good.

1 Answers1

0

The problem is that the element is created after the page is loaded. add the event to document instead of the element itself like this: You also need to set window.location.href instead of just window.location.

 $(document).on("click", ".pt-cv-ifield", function(){
 window.location.href = $(this).find("a").attr("href"); 
});

It could be you need to use jQuery() instead of $() in Wordpress.

Gert B.
  • 2,282
  • 18
  • 21