0

I'm attempting to create a javascript variable str. The ID idofnet may not exist, if it doesn't I want to ask for a value for str. If it does exist I want to pick it up from the ID. Here is my code...

function ics214button() {
    if (typeof $("idofnet").html() === "undefined") {
        var str = prompt("Enter a Log number.");
    } else {
        var str = $("#idofnet").html().trim();
    }

    if (str =="") {alert("Sorry no net was selected");}
    else {alert ("It worked");}
}

When I test this knowing there is no ID = idofnet, I get the prompt to enter a log number. And the rest of the code executes properly. But when the idofnet does exist and it contains a value, I still get the prompt asking enter a log number. The value is never set in the else condition testing for undefined. If idofnet contains a value, why is it still asking me as if it were undefined? The var str will always be a number.

Keith D Kaiser
  • 1,016
  • 2
  • 14
  • 31
  • 4
    Your first selector is missing the `#` on it. – Taplar Sep 14 '18 at 18:11
  • 1
    https://stackoverflow.com/questions/31044/is-there-an-exists-function-for-jquery – Paul Abbott Sep 14 '18 at 18:16
  • 3
    Well technically the if else could be reduced to `var str = $('#idofnet').html().trim() || prompt("Enter a Log number.");` – Taplar Sep 14 '18 at 18:17
  • 1
    @Taplar `TypeError: $('#idofnet').html().trim() is undefined` if `#idofnet` isn't on the page – VLAZ Sep 14 '18 at 18:28
  • 1
    @vlaz so you could take off the `trim()`, and then the falsy nature of undefined would work as expected. Would just need to trim whatever you put in the field in the first place. – Taplar Sep 14 '18 at 18:31
  • OK, so now if `#idofnet` exists and somehow contains a bunch of whitespace, the oneliner doesn't work. – VLAZ Sep 14 '18 at 18:36
  • @vlaz Thus my "Would just need to..." part of the comment... :| – Taplar Sep 14 '18 at 18:37
  • What if it's not an option? For example, if it's being injected by code outside your control? You keep making assumptions. – VLAZ Sep 14 '18 at 18:40
  • @vlaz You also have made plenty of assumptions in your line of contrarian responses. I only made a suggestion. – Taplar Sep 14 '18 at 18:51
  • OP says that `#idofnet` might not be on the page. I did not assume that. While I do assume it might have whitespace, I'm basing it on the fact that OP also trims the content of the element. The same reason I have to think that OP might not control the value of the element. You assumed incorrectly that the element will always be there, it'd be completely empty, and that OP has complete control over it. There is a difference. If you want to suggest a better way to do something, you should do it based on *something*. – VLAZ Sep 14 '18 at 18:57

1 Answers1

1

Changing the if condition test to this:

if ( $('#idofnet').length ) {
        var str  = $("#idofnet").html().trim();
    } else {
        var str = prompt("Enter a Log number.");
    }

Solved the problem.

Keith D Kaiser
  • 1,016
  • 2
  • 14
  • 31