ChickenLeg
ChickenLeg

Reputation: 377

I cannot get into the next element with jquery after click

I have a code, and I'm trying to get into the next element, but I cannot. Some ideas?

HTML

<label class="right">
  <input>
</label>

<label>
  <input>
</label>

CSS

 .right {
  float: right;
}

jQuery

var next = $(".right").next();
var btn = $("button");

btn.on("click", function(){
    next.addClass("right");
})

jsfiddle: https://jsfiddle.net/vg5ngoyq/1/

Upvotes: 0

Views: 40

Answers (2)

Rion Williams
Rion Williams

Reputation: 76557

Your example didn't appear to have jQuery defined prior to actually calling your jQuery-based code. After adding it in, it should work as expected :

<!-- Make sure you have something like this defined before ever calling jQuery code-->
<script src="https://code.jquery.com/jquery-2.1.4.js"></script>
<script>
    $(function(){
         // It is now safe to use your jQuery code here...
    });
</script>

Example

enter image description here

var next = $(".right").next();
var btn = $("button");

btn.on("click", function() {
  next.addClass("right");
})
.right {
  float: right;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="right">
  <input>
</label>

<label>
  <input>
</label>

<button>CLICK</button>

Upvotes: 2

The F
The F

Reputation: 3714

You simply missed including jquery in your fiddle.

Outside of jsFiddle, include jquery like so, for example:

<script src="http://code.jquery.com/jquery-1.12.4.min.js"></script>

have a look

Upvotes: 1

Related Questions