skip to Main Content

I am using a WordPress website and trying to attach a url to a button using JavaScript. I have written a function to this, My function as follows.

<div class="the_course_button" data-id="3565"><div class="course_button full button"><a href="https://gutsycreatives.com/course/writing-game-gala/?renew">TAKE THIS COURSE</a></div></div>

  jQuery(document).ready(function ($) {
 $('#3565').each(function() {
  var link = $(this).html();
  $(this).contents().wrap('<a href="https://gutsycreatives.com/product/writing-game-gala/"></a>');
 });
});

I passed my data-id ‘#3565’ to call the function. Now I am getting nothing

What did I do wrong here?

I appreciate the help thanks so much.

2

Answers


  1. That syntax you are using, is from JQuery, so the error you are getting, is problably because you haven’t linked it correctly.

    If you want to use JavaScript For making this, you should use document.getElementById("3565")

    You will need to change more things if you want to use JavScript, but if you need to use JQuery, you should look this link https://www.w3schools.com/jquery/jquery_get_started.asp

    Login or Signup to reply.
  2. I think you should clarify clearly on which environment you get that error.
    Since I see your tag ‘wordpress’ so I assume you get the error in wordpress.
    If so, please read this reference: $ is not a function – jQuery error
    Your code should be wrapped like this:

    jQuery(document).ready(function ($) {
     $('#3565').each(function() {
      var link = $(this).html();
      $(this).contents().wrap('<a href="https://gutsycreatives.com/product/writing-game-gala/"></a>');
     });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search