jquery - Recursive search for a div in javascript - Stack Overflow

Given an id of a div tag on my page i need to recursively search all children until i find a div with t

Given an id of a div tag on my page i need to recursively search all children until i find a div with the given text string as it content then remove the element ...

function removewhereTextIs(text){
    // ???
}

I have been playing with using jquery for this but i can't seem to get it to work ...

var divs = $('#MainContent_ui_calAvailability').find('div').get()
for (div in divs) {
    alert(div.innerHtml);
}

I even considered the each function ...

var divs = $('#MainContent_ui_calAvailability').find('div').get().each(
    function () { function () { if ($(this).innerText == 'TEXT') { $(this).parent.removeChild(this); }  }
);

I guess this is the difference between a UI programmer and a BLL programmer ...

So ...

Any genius out there wanna help me out?

EDIT:

thanks for the help guys ... none of these seem to be working ... I'm prety sure they should work though ... bit odd ... the actual markup looks like this ...

<DIV style="..." id="MainContent_ui_calAvailability">
 <DIV style="...">
  <DIV style="..." class="">
   <DIV style="...">

   </DIV>
   <DIV style="...">
    TEST
   </DIV>
  </DIV>
  ...
</DIV>

Do any of these work for you guys???

Given an id of a div tag on my page i need to recursively search all children until i find a div with the given text string as it content then remove the element ...

function removewhereTextIs(text){
    // ???
}

I have been playing with using jquery for this but i can't seem to get it to work ...

var divs = $('#MainContent_ui_calAvailability').find('div').get()
for (div in divs) {
    alert(div.innerHtml);
}

I even considered the each function ...

var divs = $('#MainContent_ui_calAvailability').find('div').get().each(
    function () { function () { if ($(this).innerText == 'TEXT') { $(this).parent.removeChild(this); }  }
);

I guess this is the difference between a UI programmer and a BLL programmer ...

So ...

Any genius out there wanna help me out?

EDIT:

thanks for the help guys ... none of these seem to be working ... I'm prety sure they should work though ... bit odd ... the actual markup looks like this ...

<DIV style="..." id="MainContent_ui_calAvailability">
 <DIV style="...">
  <DIV style="..." class="">
   <DIV style="...">

   </DIV>
   <DIV style="...">
    TEST
   </DIV>
  </DIV>
  ...
</DIV>

Do any of these work for you guys???

Share edited Jul 25, 2011 at 11:19 War asked Jul 25, 2011 at 10:49 WarWar 8,6284 gold badges52 silver badges100 bronze badges 1
  • Seems theres a lot of js wizards on here ... i'll be back for sure :) Watch this space lol – War Commented Jul 25, 2011 at 11:26
Add a ment  | 

4 Answers 4

Reset to default 3
$('#MainContent_ui_calAvailability div').each( function () {
    if ($(this).text() == 'TEXT') { $(this).remove(); }
});

A few points about your function:

1) you can bine the selector call and the find() call into one selector.

2) get() will return dom elements, not a jQuery object, so you can't call .each() on it.

3) You declared a function inside your each callback, but didn't actually execute it.

If you do allow partial instead of exact matches you can use the contains selector:

$('#MainContent_ui_calAvailability div:contains("TEXT")').remove();

No need for an each, how about something like this?

$('#MainContent_ui_calAvailability div:contains(TEXT)').remove();

check out the jsFiddle.

I would look at doing this:

$('#MainContent_ui_calAvailability div').each(function (index, item) {
  if ($(item).text() == "TEXT")
{
   $(item).remove();
}

});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744918524a4600986.html

相关推荐

  • jquery - Recursive search for a div in javascript - Stack Overflow

    Given an id of a div tag on my page i need to recursively search all children until i find a div with t

    1天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信