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,976
  • 5
  • 37
  • 56
Yetimwork Beyene
  • 2,239
  • 5
  • 27
  • 33

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
  • 90,923
  • 26
  • 142
  • 180
  • 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,900
  • 10
  • 46
  • 74
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
  • 169,008
  • 28
  • 173
  • 236