1

I'm remaking my website and want to do everything from scratch (no bootstrap, foundation, etc). Right now I'm making the navbar and I'm trying to get it to toggle slide in and out on click. For some reason click events aren't working and I'm not sure what I did wrong. I made a rough example in codepen here

HTML

<script src="jquery-3.4.1.min.js"></script>
<div class="box"></div>

CSS

.box {
  transform: translateX(0);
  width: 100px;
  height: 100px;
  background-color: red;
  transition: transform 250ms;
}
.slide {
  transform: translateX(250px);
}

JS

$(function() {
  $('.box').on('click', function(slideToggle) {
    $(this).toggleClass('slide');
  });
});
1
  • jQuery is not available, if you inspect the page you will see Uncaught ReferenceError: $ is not defined Commented Oct 8, 2019 at 5:20

5 Answers 5

2

For you codepen example:

Go to settings -> Add library -> search jquery and select.

If this is the same case with your real code:

open console -> check if $ is not defined or $ is not jquery ?

if $ not defined, add a valid jquery cdn url.

if $ is not jquery, try to find a conflicting library.

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

Comments

1

Everything is good in your code except your jQuery reference, please check your downloaded jquery path, or you can use like below, now i am using jquery directly from the jquery official site

<script src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<div class="box"></div>

extra note: found you added some new CSS3 attribute ("transition: transform 250ms;"), when ever you use the CSS3 attribute please make sure you should reset the attribute for all browsers, like wekit,moz,

thanks

2 Comments

oh gosh im an idiot haha. i was messing with the code and trying different selectors and events but never checked my reference haha. thanks!
haha no issues bro. when you have facing some issue in your code, go n check the console, you will get to know what exactly the error is, if something is undefined which means may be your reference to the function is not proper or some typo you made, if in the case of $ or jquery says its undefined which means jquery is not loaded in your page. and please add the reset for the css also otherwise your page will break in firefox and safari browsers
0

You have not loaded jQuery correctly for codepen. To add jQuery simply replace the <script src="jquery-3.4.1.min.js"></script>

with

<script src="https://code.jquery.com/jquery-1.12.3.js" integrity="sha256-1XMpEtA4eKXNNpXcJ1pmMPs8JV+nwLdEqwiJeCQEkyc=" crossorigin="anonymous"></script>

Comments

0

You can add jquery cdn manually in setting just click the setting icon and click the javascript tab then under that search the jquery and hit enter.

screenshot for jquery cdn codepen setting

Comments

0

It seems to work fine here in a snippet. If you replace your script on codepen with the vanilla version below, it works there too. (Note: only tested in Chrome)

$(function() {
  $('.box').on('click', function(slideToggle) {
    $(this).toggleClass('slide');
  });
});
.box {
  transform: translateX(0);
  width: 100px;
  height: 100px;
  background-color: red;
  transition: transform 250ms;
}

.slide {
  transform: translateX(250px);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="jquery-3.4.1.min.js"></script>
<div class="box"></div>

Alternative script for your codepen:

document.querySelector(".box").addEventListener("click", function(event){
  event.target.classList.toggle("slide");
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.