0

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.

4
  • Do you want the url to open after clicking the button ? Commented Mar 18, 2022 at 12:44
  • 1
    you are using jquery & probably you have not included it. Commented Mar 18, 2022 at 12:46
  • your selector is wrong - you have nothing with an id of 3565 (also ids are meant to be unique so there's no point doing .each on it) but your error is pointing to the fact you don't have jquery loaded Commented Mar 18, 2022 at 12:48
  • @VishalSharma Yes I want the url to open after clicking the button Commented Mar 18, 2022 at 12:50

2 Answers 2

0

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

Sign up to request clarification or add additional context in comments.

Comments

0

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>');
 });
});

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.