1

I have the following code with the intention of locating values from within a targeted div as follows:

$('#combineDiv').children().each(function(){
      if ($(this + ":first-child").attr('class') == 'combinedColumn') {
          alert('found ONE!');
     }
 });

I have a hidden input in some of the divs within the children of the div '#combineDiv'... but, I dont know how to combine the 'THIS' keyword with the appropriate selector... :(

1
  • Use .filter("selectors"),i.e. $(this).filter(":first-child").... Commented May 12, 2010 at 15:56

6 Answers 6

2

remember this is going to return an object from the DOM, so $(this + ":first-child") probably isn't going to give you what you want (it'll probably return something like [object Object]:first-child.

Instead try accessing the ID, or whatever it is you're after, off of that this object

$(this.ID + ":first-child").attr('class')

Sign up to request clarification or add additional context in comments.

Comments

0
$(this).filter(":first-child")

3 Comments

I thought this would work -- here is my html: <div style="border: 1px solid; background-color: lightyellow; display: block;" class="column" id="col0"><input type="hidden" value="FormRecordID" id="hiddenCol#col0" class="combinedColumn">FormRecordID</div> NOW I created that div with a CLICK event that inserts that hidden input into that div .... but when I try and do something like this --- alert($(this).filter(":first-child").val()); - It always comes back undefined...? ---
i think the problem is you doing a children().each and then a :first-child. Why don't you try putting all you HTML in the question then we can see from #combineDiv down
This will probably make some of you wince, but I just took the easy way out and did a couple loops to get my value: $('#combineDivInner').children().each(function(){ //If it is a column (div) then get the hidden value out of it. if ($(this).attr('class') == 'column') { colCount++; $(this).children().each(function() { if ($(this).attr('class') == 'columnHiddenVal') { formulaString += $(this).val();}}); }
0

Not sure what you're trying to do, but my assumption is that you want to look inside each child of the #combineDiv for a :first-child, that is a .combinedColumn

$("#combineDiv > * > .combinedColumn:first-child"); 

Comments

0

Use .filter("selectors"),i.e. $(this).filter(":first-child")....

In fact you could use $(this).filter(":first-child .combinedColumn") I believe.

Comments

0

use .find to travel more than one level down

$('#combineDiv').find(selector).each(
              function(){
                if ($(this + ":first-child").attr('class') == 'combinedColumn') {
                                   alert('found ONE!');
                            }
                        });

Comments

0

You can use the has method to get the elements that has children that matches a selector, intead of looping through the children yourself:

$('#combineDiv').children().has('.combinedColumn:first-child').each(function(){
  alert('found ONE!');
});

Example:

With this HTML:

<div id ="combineDiv">
  <div><span class="none">1</span><span class="none">1</span></div>
  <div><span class="none">2</span><span class="combinedColumn">2</span></div>
  <div><span class="combinedColumn">3</span><span class="none">3</span></div>
  <div><span class="combinedColumn">4</span><span class="combinedColumn">4</span></div>
</div>

This will match the third and fourth child div and make their background gray, as they have a first child with the class "combinedColumn":

$('#combineDiv').children().has('.combinedColumn:first-child').css('background', '#ccc');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.