1

my variable todoHtmlLi is undefined, really can't get it why.. I had declared it early before assign it to some html. I use console.log() to check the priority value, it work just fine..

$(document).on('click', '#addTodoBtn', function () {
    var todoDialog = {
        state0: {
            html: dialogContent,
            buttons: {
                Cancel: -1,
                Add: 0
            },
            focus: 1,
            submit: function (e, v, m, f) {
                e.preventDefault();

                var todoHtmlLi;
                var todoNameVal;
                var todoNoteVal;

                //Task Name
                todoNameVal = $("#todoName").val();
                todoNameVal.trim();

                //Note
                todoNoteVal = $("#todoNote").val();
                todoNoteVal.trim();

                //Priority 
                priority = $("#priority").val();

                if ($(priority) === 1) {
                    todoHtmlLi = "<li style='background:red'><a href='#'>" + todoNameVal + "<input type='checkbox'></a></li>"
                } else if ($(priority) === 2) {
                    todoHtmlLi = "<li style='background:green'><a href='#'>" + todoNameVal + "<input type='checkbox'></a></li>"
                } else if ($(priority) === 3) {
                    todoHtmlLi = "<li style='background:blue'><a href='#'>" + todoNameVal + "<input type='checkbox'></a></li>"
                }

                if (v == 0) {
                    if (todoNameVal !== "") {

                        $("div#tab").find('#todoUl').prepend(todoHtmlLi);

                        $.prompt.close();

                    } else {
                        $("#todoName").focus();
                    }

                } else {
                    $.prompt.close();

                }
            }
        }
    }

    $.prompt(todoDialog);
});

if(v == 0){ mean the 'yes' button is clicked

Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107

2 Answers2

1

First: You only assign values to todoHtmlLi based on comparing the return value of a call to val() (which will be a String) to a Number using === (which checks type).

Since "1" === 1 is not true, you never assign a value.

Either use ==, compare to Strings or convert to a Number.

Second: You pass the value as an argument to $, so you get a jQuery object back instead of that String. This doesn't make any sense, so don't do that.

if (priority == 1){
if (priority === "1"){
if (parseInt(priority,10) === 1){
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

Because your conditions are wrong.

see ,

  priority = $("#priority").val();

That returns a string.

Then

  if($(priority) === 1){

That is wrong ,Since 1 never equals to "1",So no condition satisfied.and it;s undefined.

Your if condition should be

   if(priority === "1"){

And also remaining if conditions needs to be change.

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307