StabDev
StabDev

Reputation: 741

find() not working properly. Cant find span

Trying to fill a span by finding it and using innterHTML but for some reason it doesn't do anything. No error, no succes, doesnt print anything. What has gone wrong?

<label class="myClass" data-id="1"><input type="checkbox" name="group" value="my_value">my value<span class="left" ></span></label>andmoreandmore

I've put it in a for loop for getting the classes

var element = $('.myClass')[i];

var numberThing = 50 - count;
$(element).find("span").innerHTML = numberThing + ' plekken vrij';

Upvotes: 0

Views: 1122

Answers (3)

Shubham Khatri
Shubham Khatri

Reputation: 281646

Use html() instead of innerHTML

var element = $('.myClass');
var count = 3;
                var numberThing = 50 - count;
                $(element).find("span").html(numberThing + ' plekken vrij');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <label class="myClass" data-id="1"><input type="checkbox" name="group" value="my_value">my value<span class="left" ></span></label>andmoreandmore

Replace

var element = $('.myClass')[i]

with

var element  = $('.myClass').eq(i);

Upvotes: 1

Angel ofDemons
Angel ofDemons

Reputation: 1267

Well what has gone wrong is that you did not select the correct span, or better said.. none at all

You need to select span[0] because you are selecting a DOM element the wrong way.

var element = $('.myClass')[i];
var numberThing = 50 - count;
$(element).find("span")[0].innerHTML = numberThing + ' plekken vrij';

Upvotes: 2

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

Just use .eq() instead of []. Because $('.myClass')[i] will return a dom element. which means for using the jquery methods, you need to wrap it with $() again. Instead of these, you can use,

$('.myClass').eq(i).find("span").html(numberThing + ' plekken vrij');

Upvotes: 0

Related Questions