views:

60

answers:

3

I have this html:

<div class="portlet-header">
    Company Information ... <button> some button here </button>
</div>
<div class="portlet-content">
    <div class="content_regular">
        <table style=" width:100%; height:100%;">
            ...content
        </table>
    </div>
</div>

how do i get the hmtl after i click the button at the portlet-header part, in this case, the table tag?

I have this jquery code to get the text on the header: $(this).parent().text(); but got nowhere in trying to retrieve the hmtl under the "content_regular" class as shown above.

Hope your wisdom can help me with this. i know this very easy to others but i am not familiar with jquery.

thanks

+3  A: 

First, use the html() function instead of the text() function. Second, point your selector to the right node, something like .portlet-content .content_regular table. I'll let you do the combining of these two yourself.

Peter Kruithof
yeah, i can use that, but on how to traverse to the content_regular div from portlet-header is the thing i wanted to know. what i need is the hmtl under the content_regular div
mcxiand
Like I said, change your selector and point it to the right node. The easy way is to use the class name itself. If the class is used multiple times however, you have to traverse from the clicked target. If this is the case, start with the target ($(this)), go up one level (portlet-header), get the next sibling (portlet-content) and look inside to get the table.
Peter Kruithof
@Peter - You need to traverse over...a selector won't do the job here, they are different concepts.
Nick Craver
thanks for your inputs, it gives light to my tiny brain. i did use the selector style. What saves my a** is the code above is wrapped by a div that has unique id to each portlet. so what i did is like thisvar divId = $(this).parent().parent().attr("id"); var contenHTML = $("#" + divId + " .contentw_regular").html();
mcxiand
@mcxiand - You don't have to do this...my answer gives you a very clear route to get the div you want, and a lot less work to get it.
Nick Craver
@Nick - My first suggested step is to traverse up, and then locate the right node. Maybe traverse isn't the appropriate word here, but the solution works.@mcxiand - Untested, but I think the following works too: $(this).parent().next('.portlet-content').find('.content_regular table').
Peter Kruithof
+3  A: 

Use .next() to get from .portlet-header to .portlet-content then use .html() rather than .text() to get the content, like this:

$(".portlet-header").click(function() {
  var html = $(this).next(".portlet-content").html();
});

If there may be something between the divs, another element, etc, use .nextAll() instead, like this: .nextAll(".portlet-content:first")

Nick Craver
@mcxiand This makes the most sense given your multiple widgets and selection based on the relative header. IF you need ALL of them on any click you can also use the .each and put them in an array.
Mark Schultheiss
i agree that this has the most sense, however i cant seems to make it work. my code goes like this, $(this).parent().next().nextAll(".contentwithscroll_regular:first").html();-i should call parent since the button is in the portlet header-then next to go to portlet header to portlet content-im confused now how to traverse from the portlet content to the inner div content_regular before calling the html()
mcxiand
got it! :)$(this).parent().next().children(".contentwithscroll_regular").html();
mcxiand
+1  A: 
$('.portlet-header button').click(function(){
   alert($('.content_regular').html())
});

nice readings

  1. .html()
  2. jQuery selectors
  3. Traversing

here is a demo

Reigel