Apply selector to elements that have already been selected











up vote
0
down vote

favorite












I feel like I am asking something really dumb but perhaps it's not my day. If I have a selected element already, e.g:



let tables = $('table');


But now I want to apply another selector like .some-class on top of those tables, but without creating a new jQuery object like this



$('table.some-class')


How would I do it?










share|improve this question
























  • May be like $(tables+'.some-class');
    – Ayaz Shah
    Nov 22 at 12:57








  • 1




    What about find()
    – Ankit Agarwal
    Nov 22 at 12:58










  • @AnkitAgarwal $('table').find('.some-class') is not the same than $('table.some-class').
    – dabadaba
    Nov 22 at 13:16















up vote
0
down vote

favorite












I feel like I am asking something really dumb but perhaps it's not my day. If I have a selected element already, e.g:



let tables = $('table');


But now I want to apply another selector like .some-class on top of those tables, but without creating a new jQuery object like this



$('table.some-class')


How would I do it?










share|improve this question
























  • May be like $(tables+'.some-class');
    – Ayaz Shah
    Nov 22 at 12:57








  • 1




    What about find()
    – Ankit Agarwal
    Nov 22 at 12:58










  • @AnkitAgarwal $('table').find('.some-class') is not the same than $('table.some-class').
    – dabadaba
    Nov 22 at 13:16













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I feel like I am asking something really dumb but perhaps it's not my day. If I have a selected element already, e.g:



let tables = $('table');


But now I want to apply another selector like .some-class on top of those tables, but without creating a new jQuery object like this



$('table.some-class')


How would I do it?










share|improve this question















I feel like I am asking something really dumb but perhaps it's not my day. If I have a selected element already, e.g:



let tables = $('table');


But now I want to apply another selector like .some-class on top of those tables, but without creating a new jQuery object like this



$('table.some-class')


How would I do it?







javascript jquery html jquery-selectors






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 13:03









Mohammad

14.1k93259




14.1k93259










asked Nov 22 at 12:56









dabadaba

3,06564086




3,06564086












  • May be like $(tables+'.some-class');
    – Ayaz Shah
    Nov 22 at 12:57








  • 1




    What about find()
    – Ankit Agarwal
    Nov 22 at 12:58










  • @AnkitAgarwal $('table').find('.some-class') is not the same than $('table.some-class').
    – dabadaba
    Nov 22 at 13:16


















  • May be like $(tables+'.some-class');
    – Ayaz Shah
    Nov 22 at 12:57








  • 1




    What about find()
    – Ankit Agarwal
    Nov 22 at 12:58










  • @AnkitAgarwal $('table').find('.some-class') is not the same than $('table.some-class').
    – dabadaba
    Nov 22 at 13:16
















May be like $(tables+'.some-class');
– Ayaz Shah
Nov 22 at 12:57






May be like $(tables+'.some-class');
– Ayaz Shah
Nov 22 at 12:57






1




1




What about find()
– Ankit Agarwal
Nov 22 at 12:58




What about find()
– Ankit Agarwal
Nov 22 at 12:58












@AnkitAgarwal $('table').find('.some-class') is not the same than $('table.some-class').
– dabadaba
Nov 22 at 13:16




@AnkitAgarwal $('table').find('.some-class') is not the same than $('table.some-class').
– dabadaba
Nov 22 at 13:16












2 Answers
2






active

oldest

votes

















up vote
1
down vote



accepted










You need to use .filter() to adding filter to variable selector.






let tables = $('table');
tables = tables.filter('.some-class');
tables.css('color', 'red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td>table</td>
</tr>
</table>
<table class="some-class">
<tr>
<td>table has .some-class</td>
</tr>
</table>








share|improve this answer




























    up vote
    0
    down vote













    With the following code you would find all elements with class .some-class under the elements with tag name table.



    let tables = $('table').find('.some-class');


    Of course, it is just for the example, otherwise you can simply do:



    let tables = $('table .some-class');


    In your question it is not clear if you want children elements or just filter the elements. If you want the tables with a given class, you would do:



    let tables = $('table').filter('.some-class');





    share|improve this answer





















    • I never mentioned children elements. I am trying to apply the selector on the original element.
      – dabadaba
      Nov 22 at 13:14











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431547%2fapply-selector-to-elements-that-have-already-been-selected%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    You need to use .filter() to adding filter to variable selector.






    let tables = $('table');
    tables = tables.filter('.some-class');
    tables.css('color', 'red');

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <table>
    <tr>
    <td>table</td>
    </tr>
    </table>
    <table class="some-class">
    <tr>
    <td>table has .some-class</td>
    </tr>
    </table>








    share|improve this answer

























      up vote
      1
      down vote



      accepted










      You need to use .filter() to adding filter to variable selector.






      let tables = $('table');
      tables = tables.filter('.some-class');
      tables.css('color', 'red');

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <table>
      <tr>
      <td>table</td>
      </tr>
      </table>
      <table class="some-class">
      <tr>
      <td>table has .some-class</td>
      </tr>
      </table>








      share|improve this answer























        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        You need to use .filter() to adding filter to variable selector.






        let tables = $('table');
        tables = tables.filter('.some-class');
        tables.css('color', 'red');

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <table>
        <tr>
        <td>table</td>
        </tr>
        </table>
        <table class="some-class">
        <tr>
        <td>table has .some-class</td>
        </tr>
        </table>








        share|improve this answer












        You need to use .filter() to adding filter to variable selector.






        let tables = $('table');
        tables = tables.filter('.some-class');
        tables.css('color', 'red');

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <table>
        <tr>
        <td>table</td>
        </tr>
        </table>
        <table class="some-class">
        <tr>
        <td>table has .some-class</td>
        </tr>
        </table>








        let tables = $('table');
        tables = tables.filter('.some-class');
        tables.css('color', 'red');

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <table>
        <tr>
        <td>table</td>
        </tr>
        </table>
        <table class="some-class">
        <tr>
        <td>table has .some-class</td>
        </tr>
        </table>





        let tables = $('table');
        tables = tables.filter('.some-class');
        tables.css('color', 'red');

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <table>
        <tr>
        <td>table</td>
        </tr>
        </table>
        <table class="some-class">
        <tr>
        <td>table has .some-class</td>
        </tr>
        </table>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 13:00









        Mohammad

        14.1k93259




        14.1k93259
























            up vote
            0
            down vote













            With the following code you would find all elements with class .some-class under the elements with tag name table.



            let tables = $('table').find('.some-class');


            Of course, it is just for the example, otherwise you can simply do:



            let tables = $('table .some-class');


            In your question it is not clear if you want children elements or just filter the elements. If you want the tables with a given class, you would do:



            let tables = $('table').filter('.some-class');





            share|improve this answer





















            • I never mentioned children elements. I am trying to apply the selector on the original element.
              – dabadaba
              Nov 22 at 13:14















            up vote
            0
            down vote













            With the following code you would find all elements with class .some-class under the elements with tag name table.



            let tables = $('table').find('.some-class');


            Of course, it is just for the example, otherwise you can simply do:



            let tables = $('table .some-class');


            In your question it is not clear if you want children elements or just filter the elements. If you want the tables with a given class, you would do:



            let tables = $('table').filter('.some-class');





            share|improve this answer





















            • I never mentioned children elements. I am trying to apply the selector on the original element.
              – dabadaba
              Nov 22 at 13:14













            up vote
            0
            down vote










            up vote
            0
            down vote









            With the following code you would find all elements with class .some-class under the elements with tag name table.



            let tables = $('table').find('.some-class');


            Of course, it is just for the example, otherwise you can simply do:



            let tables = $('table .some-class');


            In your question it is not clear if you want children elements or just filter the elements. If you want the tables with a given class, you would do:



            let tables = $('table').filter('.some-class');





            share|improve this answer












            With the following code you would find all elements with class .some-class under the elements with tag name table.



            let tables = $('table').find('.some-class');


            Of course, it is just for the example, otherwise you can simply do:



            let tables = $('table .some-class');


            In your question it is not clear if you want children elements or just filter the elements. If you want the tables with a given class, you would do:



            let tables = $('table').filter('.some-class');






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 12:58









            LaurentG

            7,86573553




            7,86573553












            • I never mentioned children elements. I am trying to apply the selector on the original element.
              – dabadaba
              Nov 22 at 13:14


















            • I never mentioned children elements. I am trying to apply the selector on the original element.
              – dabadaba
              Nov 22 at 13:14
















            I never mentioned children elements. I am trying to apply the selector on the original element.
            – dabadaba
            Nov 22 at 13:14




            I never mentioned children elements. I am trying to apply the selector on the original element.
            – dabadaba
            Nov 22 at 13:14


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431547%2fapply-selector-to-elements-that-have-already-been-selected%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Catalogne

            Violoncelliste

            Héron pourpré