If statement within function not working —












-1














I have an if statement below if ($(module).text() === '') { that I cannot get to work:



Can anyone spot what I am doing wrong here?



//button is pressed, this function is called
function submit(button) {
var module = $(button).closest().find('.Table2');
console.log(module);

//gets the text content of a previous text area
var copy = $(button).prev().val();

//checks if the previous text area is empty, if it is, removes it and its container then exits the function
if (copy == "") {
$(button).closest('tr').remove();
return;
}

//replaces all line breaks with <br>
copy = copy.replace(/(?:rn|r|n)/g, '<br>');

//remove the button and text area and insert text instead
button.parentElement.innerHTML = (copy);

//fall back if all text areas have been submitted empty target the level table and remove DOES NOT WORK
if ($(module).text() === '') {
console.log('help me get to here');
$(module).remove();
}

//wrap all occurances of <br><br> in a span so that height of line breaks can be set
var content = document.getElementById('content').innerHTML;
var breakReplace = content.replace(/<br><br>/g, '<span style="line-height:14px;"><br><br></span>');
document.getElementById('content').innerHTML = breakReplace;
}









share|improve this question
























  • What appears in the console if you do console.log($(module).text())? I would guess that you're missing a call to trim()
    – Rory McCrossan
    Nov 16 at 10:04








  • 1




    $(module) wraps a jQuery object in a jQuery object. module.text() === '' should be sufficient.
    – connexo
    Nov 16 at 10:04










  • It can be possible that it can't find .Table2
    – Jelle Botman
    Nov 16 at 10:04










  • @connexo yes thats right!
    – Jelle Botman
    Nov 16 at 10:05






  • 1




    @KirankumarDafda Minimal, Complete, and Verifiable example If he can't do that, then he needs to ask this elsewhere.
    – Rob
    Nov 16 at 12:35


















-1














I have an if statement below if ($(module).text() === '') { that I cannot get to work:



Can anyone spot what I am doing wrong here?



//button is pressed, this function is called
function submit(button) {
var module = $(button).closest().find('.Table2');
console.log(module);

//gets the text content of a previous text area
var copy = $(button).prev().val();

//checks if the previous text area is empty, if it is, removes it and its container then exits the function
if (copy == "") {
$(button).closest('tr').remove();
return;
}

//replaces all line breaks with <br>
copy = copy.replace(/(?:rn|r|n)/g, '<br>');

//remove the button and text area and insert text instead
button.parentElement.innerHTML = (copy);

//fall back if all text areas have been submitted empty target the level table and remove DOES NOT WORK
if ($(module).text() === '') {
console.log('help me get to here');
$(module).remove();
}

//wrap all occurances of <br><br> in a span so that height of line breaks can be set
var content = document.getElementById('content').innerHTML;
var breakReplace = content.replace(/<br><br>/g, '<span style="line-height:14px;"><br><br></span>');
document.getElementById('content').innerHTML = breakReplace;
}









share|improve this question
























  • What appears in the console if you do console.log($(module).text())? I would guess that you're missing a call to trim()
    – Rory McCrossan
    Nov 16 at 10:04








  • 1




    $(module) wraps a jQuery object in a jQuery object. module.text() === '' should be sufficient.
    – connexo
    Nov 16 at 10:04










  • It can be possible that it can't find .Table2
    – Jelle Botman
    Nov 16 at 10:04










  • @connexo yes thats right!
    – Jelle Botman
    Nov 16 at 10:05






  • 1




    @KirankumarDafda Minimal, Complete, and Verifiable example If he can't do that, then he needs to ask this elsewhere.
    – Rob
    Nov 16 at 12:35
















-1












-1








-1







I have an if statement below if ($(module).text() === '') { that I cannot get to work:



Can anyone spot what I am doing wrong here?



//button is pressed, this function is called
function submit(button) {
var module = $(button).closest().find('.Table2');
console.log(module);

//gets the text content of a previous text area
var copy = $(button).prev().val();

//checks if the previous text area is empty, if it is, removes it and its container then exits the function
if (copy == "") {
$(button).closest('tr').remove();
return;
}

//replaces all line breaks with <br>
copy = copy.replace(/(?:rn|r|n)/g, '<br>');

//remove the button and text area and insert text instead
button.parentElement.innerHTML = (copy);

//fall back if all text areas have been submitted empty target the level table and remove DOES NOT WORK
if ($(module).text() === '') {
console.log('help me get to here');
$(module).remove();
}

//wrap all occurances of <br><br> in a span so that height of line breaks can be set
var content = document.getElementById('content').innerHTML;
var breakReplace = content.replace(/<br><br>/g, '<span style="line-height:14px;"><br><br></span>');
document.getElementById('content').innerHTML = breakReplace;
}









share|improve this question















I have an if statement below if ($(module).text() === '') { that I cannot get to work:



Can anyone spot what I am doing wrong here?



//button is pressed, this function is called
function submit(button) {
var module = $(button).closest().find('.Table2');
console.log(module);

//gets the text content of a previous text area
var copy = $(button).prev().val();

//checks if the previous text area is empty, if it is, removes it and its container then exits the function
if (copy == "") {
$(button).closest('tr').remove();
return;
}

//replaces all line breaks with <br>
copy = copy.replace(/(?:rn|r|n)/g, '<br>');

//remove the button and text area and insert text instead
button.parentElement.innerHTML = (copy);

//fall back if all text areas have been submitted empty target the level table and remove DOES NOT WORK
if ($(module).text() === '') {
console.log('help me get to here');
$(module).remove();
}

//wrap all occurances of <br><br> in a span so that height of line breaks can be set
var content = document.getElementById('content').innerHTML;
var breakReplace = content.replace(/<br><br>/g, '<span style="line-height:14px;"><br><br></span>');
document.getElementById('content').innerHTML = breakReplace;
}






javascript jquery html






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 at 10:03









Rory McCrossan

241k29206245




241k29206245










asked Nov 16 at 10:01









Neil Morgan

265




265












  • What appears in the console if you do console.log($(module).text())? I would guess that you're missing a call to trim()
    – Rory McCrossan
    Nov 16 at 10:04








  • 1




    $(module) wraps a jQuery object in a jQuery object. module.text() === '' should be sufficient.
    – connexo
    Nov 16 at 10:04










  • It can be possible that it can't find .Table2
    – Jelle Botman
    Nov 16 at 10:04










  • @connexo yes thats right!
    – Jelle Botman
    Nov 16 at 10:05






  • 1




    @KirankumarDafda Minimal, Complete, and Verifiable example If he can't do that, then he needs to ask this elsewhere.
    – Rob
    Nov 16 at 12:35




















  • What appears in the console if you do console.log($(module).text())? I would guess that you're missing a call to trim()
    – Rory McCrossan
    Nov 16 at 10:04








  • 1




    $(module) wraps a jQuery object in a jQuery object. module.text() === '' should be sufficient.
    – connexo
    Nov 16 at 10:04










  • It can be possible that it can't find .Table2
    – Jelle Botman
    Nov 16 at 10:04










  • @connexo yes thats right!
    – Jelle Botman
    Nov 16 at 10:05






  • 1




    @KirankumarDafda Minimal, Complete, and Verifiable example If he can't do that, then he needs to ask this elsewhere.
    – Rob
    Nov 16 at 12:35


















What appears in the console if you do console.log($(module).text())? I would guess that you're missing a call to trim()
– Rory McCrossan
Nov 16 at 10:04






What appears in the console if you do console.log($(module).text())? I would guess that you're missing a call to trim()
– Rory McCrossan
Nov 16 at 10:04






1




1




$(module) wraps a jQuery object in a jQuery object. module.text() === '' should be sufficient.
– connexo
Nov 16 at 10:04




$(module) wraps a jQuery object in a jQuery object. module.text() === '' should be sufficient.
– connexo
Nov 16 at 10:04












It can be possible that it can't find .Table2
– Jelle Botman
Nov 16 at 10:04




It can be possible that it can't find .Table2
– Jelle Botman
Nov 16 at 10:04












@connexo yes thats right!
– Jelle Botman
Nov 16 at 10:05




@connexo yes thats right!
– Jelle Botman
Nov 16 at 10:05




1




1




@KirankumarDafda Minimal, Complete, and Verifiable example If he can't do that, then he needs to ask this elsewhere.
– Rob
Nov 16 at 12:35






@KirankumarDafda Minimal, Complete, and Verifiable example If he can't do that, then he needs to ask this elsewhere.
– Rob
Nov 16 at 12:35














2 Answers
2






active

oldest

votes


















0














I finally figured out that the problem was the return in



if (copy == "") {
$(button).closest('tr').remove();
return;


}



Removing the return has fixed the issue.






share|improve this answer





























    -1














    $(module)
    make sure module is not undefined



    Also JQuery Selector ('#' and '.') are required to select element






    share|improve this answer





















    • Can you please elaborate the last sentence? There are are a ton of selectors working fine without id or class selector.
      – Teemu
      Nov 16 at 10:12












    • w3schools.com/jquery/jquery_ref_selectors.asp
      – Tauqeer
      Nov 16 at 10:20










    • You've somehow misunderstood that page. There's only a couple of id or class selectors, the most of them seem to be pseudo-class selectors. Please consult jQuery documentation rather than W3Schools ...
      – Teemu
      Nov 16 at 10:24













    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',
    autoActivateHeartbeat: false,
    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%2f53335456%2fif-statement-within-function-not-working%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









    0














    I finally figured out that the problem was the return in



    if (copy == "") {
    $(button).closest('tr').remove();
    return;


    }



    Removing the return has fixed the issue.






    share|improve this answer


























      0














      I finally figured out that the problem was the return in



      if (copy == "") {
      $(button).closest('tr').remove();
      return;


      }



      Removing the return has fixed the issue.






      share|improve this answer
























        0












        0








        0






        I finally figured out that the problem was the return in



        if (copy == "") {
        $(button).closest('tr').remove();
        return;


        }



        Removing the return has fixed the issue.






        share|improve this answer












        I finally figured out that the problem was the return in



        if (copy == "") {
        $(button).closest('tr').remove();
        return;


        }



        Removing the return has fixed the issue.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 13:44









        Neil Morgan

        265




        265

























            -1














            $(module)
            make sure module is not undefined



            Also JQuery Selector ('#' and '.') are required to select element






            share|improve this answer





















            • Can you please elaborate the last sentence? There are are a ton of selectors working fine without id or class selector.
              – Teemu
              Nov 16 at 10:12












            • w3schools.com/jquery/jquery_ref_selectors.asp
              – Tauqeer
              Nov 16 at 10:20










            • You've somehow misunderstood that page. There's only a couple of id or class selectors, the most of them seem to be pseudo-class selectors. Please consult jQuery documentation rather than W3Schools ...
              – Teemu
              Nov 16 at 10:24


















            -1














            $(module)
            make sure module is not undefined



            Also JQuery Selector ('#' and '.') are required to select element






            share|improve this answer





















            • Can you please elaborate the last sentence? There are are a ton of selectors working fine without id or class selector.
              – Teemu
              Nov 16 at 10:12












            • w3schools.com/jquery/jquery_ref_selectors.asp
              – Tauqeer
              Nov 16 at 10:20










            • You've somehow misunderstood that page. There's only a couple of id or class selectors, the most of them seem to be pseudo-class selectors. Please consult jQuery documentation rather than W3Schools ...
              – Teemu
              Nov 16 at 10:24
















            -1












            -1








            -1






            $(module)
            make sure module is not undefined



            Also JQuery Selector ('#' and '.') are required to select element






            share|improve this answer












            $(module)
            make sure module is not undefined



            Also JQuery Selector ('#' and '.') are required to select element







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 at 10:07









            Tauqeer

            91




            91












            • Can you please elaborate the last sentence? There are are a ton of selectors working fine without id or class selector.
              – Teemu
              Nov 16 at 10:12












            • w3schools.com/jquery/jquery_ref_selectors.asp
              – Tauqeer
              Nov 16 at 10:20










            • You've somehow misunderstood that page. There's only a couple of id or class selectors, the most of them seem to be pseudo-class selectors. Please consult jQuery documentation rather than W3Schools ...
              – Teemu
              Nov 16 at 10:24




















            • Can you please elaborate the last sentence? There are are a ton of selectors working fine without id or class selector.
              – Teemu
              Nov 16 at 10:12












            • w3schools.com/jquery/jquery_ref_selectors.asp
              – Tauqeer
              Nov 16 at 10:20










            • You've somehow misunderstood that page. There's only a couple of id or class selectors, the most of them seem to be pseudo-class selectors. Please consult jQuery documentation rather than W3Schools ...
              – Teemu
              Nov 16 at 10:24


















            Can you please elaborate the last sentence? There are are a ton of selectors working fine without id or class selector.
            – Teemu
            Nov 16 at 10:12






            Can you please elaborate the last sentence? There are are a ton of selectors working fine without id or class selector.
            – Teemu
            Nov 16 at 10:12














            w3schools.com/jquery/jquery_ref_selectors.asp
            – Tauqeer
            Nov 16 at 10:20




            w3schools.com/jquery/jquery_ref_selectors.asp
            – Tauqeer
            Nov 16 at 10:20












            You've somehow misunderstood that page. There's only a couple of id or class selectors, the most of them seem to be pseudo-class selectors. Please consult jQuery documentation rather than W3Schools ...
            – Teemu
            Nov 16 at 10:24






            You've somehow misunderstood that page. There's only a couple of id or class selectors, the most of them seem to be pseudo-class selectors. Please consult jQuery documentation rather than W3Schools ...
            – Teemu
            Nov 16 at 10:24




















            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%2f53335456%2fif-statement-within-function-not-working%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

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?