0

I'm completely new in js and jquery. While trying to understand it, I've came up with an issue. But before that, I would like to apologise if my question contains subquestions.

First of all, I saw in this question that, .checked should be used with DOM objects while .attr() needs to be used with jquery objects. Now my question:

   <html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>
    <ul>
        <li>List element 1</li>
        <li>List element 2</li>
    </ul>
    checkbox1:<input type='checkbox' id='checkbox1'/>
    checkbox2:<input type='checkbox' id='checkbox2'/>
    <script>
        var checkboxes=$('input');
        checkboxes[1].checked=true;
    </script>
</body>   
</html>

IN here, Does checkboxes variable is a jquery object or dom element ? I was thinking that $() returns a jquery object (as stated here) but when I try, checkboxes.attr('checked',true) rather than checkboxes[1].checked=true; , I got error. My another assumption is that, may be checkboxes variable is a jquery object and checkboxes[1] is an dom element? Am I right?

Edit

One more question, when I want to learn type of a variable, I'm writing browser's console this statement : typeof(VariableName). Unfortunatelly, When I write typeof(checkboxes) or typeof(checkboxes1), I got always Object result. But just know I learn that one of them is Jquery object and the other is DOM object. Is there any function which gives me these differences?

Community
  • 1
  • 1
zwlayer
  • 1,752
  • 1
  • 18
  • 41
  • 3
    Yes, you’re right. To get a jQuery item at an index from a jQuery collection, use `.eq`, as in `checkboxes.eq(1).prop('checked', true)`. (And use `prop` to set properties, not `attr`!) – Ry- Sep 05 '15 at 09:30
  • 2
    in jquery you use the `prop` so just change `attr to prop` – guradio Sep 05 '15 at 09:30
  • @minitech thank you so much, if you can write it as an answer, I'd accept it. And yes, next think that I'll learn is difference between attr and prop – zwlayer Sep 05 '15 at 09:31
  • 1
    `jquery` before 1.6 using `attr` and `jquery` from 1.6 using `prop` ^^ – Neo Sep 05 '15 at 09:36
  • @minitech I edited my question, could you check it out again ? – zwlayer Sep 05 '15 at 09:37

1 Answers1

1

one of them is Jquery object and the other is DOM object. Is there any function which gives me these differences?

You can use:

obj instanceof jQuery

If you want more universal way you can use:

Object.prototype.toString.call(myVariable);
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
  • thank you for your reply. But to use it, I need to know my options to check with instanceof. I'm asking how could I know the type of a variable if I do not know anything special about it – zwlayer Sep 05 '15 at 11:37
  • If your variable is called `myVariable` then you can check type using `if myVariable instanceof jQuery`. Simple as that. – Daniel Kmak Sep 05 '15 at 11:49
  • I think you could not understand my question. Lets assume you have var a= 15; but you you do not know what is the type of 15. So in this case, I want to learn that the type of a. For example in python I would use type(a) and it prints or something else. Is there anything something like that in JS ? – zwlayer Sep 05 '15 at 12:48
  • 1
    If you don't want `typeof()` (JavaScript has only small list of types), you can check for object's name or constructor - here you have more info - http://stackoverflow.com/a/332429/2166409. – Daniel Kmak Sep 05 '15 at 12:56