tags:

views:

755

answers:

6

Hi guys, This might sound a little dumb, but can you please point out why in the following script 'firstValue' is undefined and 'secondValue' is as needed, i.e. 4.

<div >
<input class="feedback-selected"  type="hidden" value="4" />
</div>    
<script type="text/javascript">

    var firstValue = $(this).find('.feedback-selected').val();
    var secondValue = $('.feedback-selected').val();
    alert(firstValue);
    alert(secondValue);

</script>

I am sure I am not using the find function as it should be.

+4  A: 

this doesn't have a value in your example, so the find() function will not find anything.

(this is only valid in instance functions or event handlers)

Philippe Leybaert
The edit made to my answer is not correct. $(this) does have a value, although with no DOM elements in it, but "this" is undefined (and that's the reason for the error the OP is getting)
Philippe Leybaert
I didn't change that wording. I changed from just "this" to "$(this)" as the two are different things.
Garry Shutler
Thanks dude, I knew I was doing something dumb.
theraneman
I wanted to point out that "this" was undefined (no value). If I would have said "$(this) doesn't have a value", that would be wrong, because it does. $(undefined) returns a valid jQuery object
Philippe Leybaert
But even saying this is only valid in instance functions or event handlers doesn't quite address it's usage in the example. What matters is what it references. Does it reference a DOM element? "this" can be manipulated with apply. Might be a good idea to reference the other answer as well that talks about "this" as used by jQuery.
altCognito
A: 

What activa said... and you could probably use $(document) instead of $(this) if you need to use .find()

veggerby
I believe $().find(); might work too...?
Antony Carthy
A: 

activa's answer is correct. $("<selector>") is the equivalent of what you were trying to do with the first search. find("<selector>") is meant for searching within a wrapped set, not as a starting point.

However, I would add that a better way of retrieving the value in this case is:

var value = $("input.feedback-selected").val();

This is because including the type of the element will allow jQuery to search the DOM in a quicker way.

Garry Shutler
A: 

find() searches for descendant elements of the jQuery object you call it on. You call find() on the results of $(this), which is nothing. If you want to call find() on the document, you'll have to use $(document).

Lodewijk
A: 

this in your example is not a DOM element that contains any subelements of class "feedback-selected", so the find function can't find them. It might not even be a DOM element.

I would note that $(this) can be valid anywhere this points to a DOM element. So if pretty much anytime you're triggering a function that is called from a DOM element, you're fine. It's all about the scope in this case. This would be valid too:

function aha() {
 alert($(this).find('.feedback-selected').val());
}

aha.apply(document);
altCognito
+1  A: 

Explanation of what 'this' means in jQuery

Rick J