2

I have the following content:

<input type='checkbox' id='inputCheckbox'/><strong>sdfsf</strong><p>dfsfsdsd</p>

From the elements above, I only need to get these elements when the checkbox are selected and omit the input element:

<strong>sdfsf</strong><p>dfsfsdsd</p>

My attempt:

$("#testPatternButton").on("click", function()
{
    $("input:checked").each(function()
    {
        alert($(this).parent().children().not("input").html()); // only the strong text shows up
    });
});
embedded.kyle
  • 10,426
  • 5
  • 35
  • 56
Yetimwork Beyene
  • 2,189
  • 4
  • 26
  • 31

4 Answers4

1

This will do it -- it uses a common trick to get the .html() of the input, then uses .replace to remove that string from the parent .html().

$('input').on('click', function () {
    inputhtml = $(this).clone().wrap('<div>').parent().html();
    allhtml = $(this).parent().html();
    str = allhtml.replace(inputhtml, '');
    alert(str);
});

http://jsfiddle.net/jnabj/

If, however, you really want a jQuery object of those elements instead of the HTML string, that's even easier:

$('input').on('click',function() {
    $var = $(this).siblings();
});
Community
  • 1
  • 1
Blazemonger
  • 86,267
  • 25
  • 136
  • 177
  • Siblings is the most elegant. Interestingly enough it does not seem to take anything before the checkbox – mplungjan Feb 01 '13 at 14:34
1

Use each() and add up the html() contents.

$("#testPatternButton").on("click", function() {
    $("input:checked").each(function() {
        var output = '';
        $(this).parent().children().not("input").each(function() {
            output = output + $(this).html();
        });
        alert(output);
    });
});
Antony
  • 14,670
  • 10
  • 42
  • 73
1

Try this.

$("#inputCheckbox").click(function(){
    var el = this;
    if (el.checked) {
        console.log(
            $(el).nextAll()
        );
    }
});
0

This works

DEMO

$("#testPatternButton").on("click", function() {
    $("input:checked").each(function() {
      $("#out").html($(this).parent().contents().not('input')); 
    });
});
mplungjan
  • 155,085
  • 27
  • 166
  • 222