0

I am somewhat new to jquery.

Can any one tell me what's wrong with this?

var l = $(target)+".thumb".length;

The console log returns this:

[object Object]6

I have the "target" var set up ahead of the "l" var (along with another var) like this:

var target = $(this).parent();
var n = $(".thumb").outerWidth(true);
var l = $(target)+".thumb".length;

I'm not sure what "[obbject Object] in the log means.

belotte
  • 23
  • 4
  • `[object Object]` means you have an object, ie something that isn't just a string or an int (or other primitive type). In this case it's a jquery object. Exactly what are you trying to do with the `target`? – freedomn-m Apr 11 '18 at 06:35
  • If you want to know what it is, then learn how to debug your code and inspect the variable. You can put the keyword `debugger;` directly above your code and the browser will go into debugging mode for you. Or you could add `console.log($(target))` to see what it is (don't use `alert()` as it will show `[Object]` again). – freedomn-m Apr 11 '18 at 06:37
  • Thank you, everyone, for your help. – belotte Apr 18 '18 at 14:03

2 Answers2

0
var target = $(this).parent();
var l = $(target)+".thumb".length;

target is object at first line, the second line replace by

var l = target+".thumb".length;

maybe ok

Ivan
  • 89
  • 5
0

You can use find rather than concat string to get length.

$("#btnTest").on("click", function() {
  var target = $(this).parent();
  var n = $(".thumb").outerWidth(true);
  var l = $(target).find(".thumb").length;
  console.log(l);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <div class='thumb'></div>
  <input type='button' id='btnTest' value='Test' />
  <div class='thumb'></div>
</div>
4b0
  • 21,981
  • 30
  • 95
  • 142
  • This assumes `.thumb` is a child of the event's parent. Without any html or further clarification from OP, this is just a guess (a possibility, but still a guess). It's just as likely that the thumbs are in a completely different location in the html. Who knows? OP knows, but isn't telling. – freedomn-m Apr 11 '18 at 07:04
  • Thank you all for your help. – belotte Apr 18 '18 at 14:04