tags:

views:

193

answers:

4

Hello Friends,

 <fieldset id="fs1">
            <legend id="leg1">First Legend</legend>
            <div id="div1">
                "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
                laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi                   
                 <div id="div1Child">
                      ********* Sample text here ****************
                  </div>
            </div>

        </fieldset>
        <fieldset id="fs2">
            <legend id="leg2">Second Legend</legend>
            <div id="div2">
                "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium
                voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint

                  <div id="div2Child">

                      ********* Sample text here ****************
                 </div>
            </div>

        </fieldset>

When I click the legend, I should get the contents of the first div (div1 and div2) only. I don't want to display the "Sample Text Here" which is the child div(Id -->> div1Child and div2Child) of the div. Any help would be appreciated. It's pretty urgent.

Thanks in advance.

A: 

$("leg1").next() and $("leg2").next() ?

See here

kripto_ash
A: 

Try this:

$("fieldset > div:first-child")
NawaMan
+3  A: 

Here is code on how you can handle showing the first parent div on legend click, and then showing the child div of the parent div with clicking a button "read more"...

<script>
    $(document).ready(function() {
        $("fieldset legend").click(function() {
            $(this).next("div").toggle().children("div").each(function() {
                $(this).hide();
            });
        });
        $(".btnreadmore").click(function() {
            $(this).next("div").show();
        });
    });
</script>

<fieldset id="fs1">
        <legend id="leg1">First Legend</legend>
        <div id="div1" class="hidden">
            "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque
            laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi. 
            <input type="button" value="read more" class="btnreadmore" />                  
             <div id="div1Child">
                  ********* Sample text here ****************
              </div>
        </div>
    </fieldset>
<fieldset id="fs2">
        <legend id="leg2">Second Legend</legend>
        <div id="div2" class="hidden">
            "At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium
            voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint
            <input type="button" value="read more" class="btnreadmore" />                  
              <div id="div2Child">
                  ********* Sample text here ****************
             </div>
        </div>
    </fieldset>
RSolberg
Yeah, I use this code already. But I don't want to allow other div's inside to toggle().
engineerachu
Yeah, as per your code, it will hide all the divs inside "div1". But when div1Child is clicked, I shouldn't allow them to toggle(). I'm trying to write a common method for whole application. Any idea?
engineerachu
No no. toggle() only fires when legend is clicked.
engineerachu
@engineerachu: Your question says you want to toggle the first DIV for the legend on click.
RSolberg
You are absolutely correct.
engineerachu
Thanks. I'll check it and let you know.
engineerachu
+1  A: 

This is the best I can think of..

$('legend').click(function(e){
     var $div = $(this).next('div').clone();
     var text = $div.children().remove().end().text();
     console.log(text);
});

EDIT: The above code assumes that:

  1. you don't want anything inside your <div id="div1"> except the immediate text, and
  2. that you're using Firebug to develop your code.
dalbaeb
Hello dalbaeb, You are absolutely correct. I get the text of the first div only. Now my exact scenario is to toggle the css->display to "block" and "none" when clicking of the div. When I click on "div1", all contents should toggle the display css (block and none), but when I click on "div1Child", it shouldn't happen. You have any idea? I tried as you said, but it doesn't seem to accept the css property.
engineerachu
Sorry, I don't understand. This completely changes the task. What are you clicking on, and what do you want to hide/show? If you want the text in div#div1 to be hidden when you click within div#div1 then you should consider wrapping the text in a span or something, and toggle that span.
dalbaeb
Okay, I'll make it clear. From the above html sample, when I click on "div1", all the contents will toggle(). When I click on "div1Child", that div should not toggle. I'm trying to write a common method to work all over the application.
engineerachu
1: You should edit the question. 2: Wrap the content you want to toggle into a span, and toggle it instead of the whole DIV (assuming you have control over HTML markup). Otherwise I really don't know what you're going to click on after having hid the entire DIV once.
dalbaeb