Link to home
Start Free TrialLog in
Avatar of Peter Chan
Peter ChanFlag for Hong Kong

asked on

Correct JS

Hi,
How to correct code below?

User generated image
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

can you paste the code as codes instead of image here?
You are missing a closing } to close the anonymous function on the change event

The second to last line should be }}) to close the if statement and the function declaration.
Avatar of Peter Chan

ASKER

Thanks to all.
Jeffrey,
By that part of JS codes, I want to disable button below (when readonly=y)
User generated image
but it cannot do that below. What to adjust?
http://my-friend.co/UserMaintenance2/Default.aspx?userid=mc4&readonly=y
I guess this code you need to write on document.ready
var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            $("#tb_birth").datepicker("disable");
        }

Open in new window

Is it to put such codes to Java script in above? How to adjust the above codes? Where is document.ready?
What you are trying to do is fine. I meant the syntax of your JavaScript is incorrect.
At the end it should be
        if (isreadonly == "y") {
            $("#tb_birth").datepicker("disable");
        }})

Open in new window


See how I added an extra }, that is to close the function you opened earlier
Yes, I have codes like
$(function () {
    $("#tb_birth").datepicker({
        dateFormat: "dd/mm/yy",
        changeYear: true,
        showOn: "button",
    }).next('button').text('').button({
        icons: {
            primary: 'ui-icon-calendar'
        },
        label: "选择日期",
        text: false
    }).on("change", function () {
        var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            $("#mybutton").attr("disabled", "disabled")
        }
        var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            $("#tb_birth").datepicker("disable");
        }
    });
    });

Open in new window

but after deployment, I still cannot disable the button next to date field below.

http://my-friend.co/UserMaintenance2/Default.aspx?userid=mc4&readonly=y
Firstly you have this
var isreadonly = getUrlParameter('readonly');

Open in new window

Which is INSIDE the .on("change") so the check for readonly is only going to happen when you actually change the date.

If you want to disable the button based on the URL parameter at page load you have to move this OUTSIDE of the function chain you have going

Then you can optimise your code to do this
// ONLY GET THIS ONCE
 var isreadonly = getUrlParameter('readonly');
if (isreadonly == "y") {
  // USE prop RATHER THAN attr AND SET IT TO TRUE
  $("#mybutton").prop("disabled", true)
 // DISABLE THE DATEPICKER
  $("#tb_birth").datepicker("option", { disabled: true });
}

Open in new window

However, we can make this even simpler. Firstly, there is no id="mybutton" in your code so that line is totally useless.
Secondly, if you disable the datepicker the button will no longer open the DatePicker and is effectively disabled. You can therefore just do this
$(function() {
  // ONLY GET THIS ONCE
 var isreadonly = getUrlParameter('readonly');
  if (isreadonly == "y") {
    // DISABLE THE DATEPICKER
    $("#tb_birth").datepicker("option", { disabled: true });
  }
})

Open in new window


If you want to disable the button then use the first code listing making sure you reference an id that actually exists in your page.

Some things to remember

When disabling controls use .prop() and remember that disabled is a Boolean and should be set to true or false.
Thanks to all.
Julian,
Here is the current codes
$(function () {
    $("#tb_birth").datepicker({
        dateFormat: "dd/mm/yy",
        changeYear: true,
        showOn: "button",
    }).next('button').text('').button({
        icons: {
            primary: 'ui-icon-calendar'
        },
        label: "选择日期",
        text: false
    }).on("change", function () {
        var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            // DISABLE THE DATEPICKER
            $("#tb_birth").datepicker("option", { disabled: true });
        }
    });
});

Open in new window

but I still cannot disable the datepicker, after re-deployment, below.

http://my-friend.co/UserMaintenance2/Default.aspx?userid=mc4&readonly=y
Please read my previous comment

YOU ARE PUTTING YOUR DISABLE CODE INSIDE AN ONCHANGE


}).on("change", function () {
 // ************************************
 // THIS CODE WILL ONLY FIRE WHEN YOU CHANGE THE VALUE IN THE DATEPICKER - IS THIS WHAT YOU WANT?
 // ************************************
        var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            // DISABLE THE DATEPICKER
            $("#tb_birth").datepicker("option", { disabled: true });
        }
    });

Open in new window

How to ensure to "get your codes once"?
I don't understand the question.
Per your advice

// ONLY GET THIS ONCE
 var isreadonly = getUrlParameter('readonly');
if (isreadonly == "y") {
  // USE prop RATHER THAN attr AND SET IT TO TRUE
  $("#mybutton").prop("disabled", true)
 // DISABLE THE DATEPICKER
  $("#tb_birth").datepicker("option", { disabled: true });
}

what do you mean by

"ONLY GET THIS ONCE"?
In your original code you had
}).on("change", function () {
       // THIS LINE IS IDENTICAL ....
        var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            $("#mybutton").attr("disabled", "disabled")
        }
       // TO THIS ONE. 
       // THIS LINE IS UNNECESSARY - YOU ALREADY HAVE THIS VALUE ABOVE
        var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            $("#tb_birth").datepicker("disable");
        }
    });
    });

Open in new window

Using these codes
$(function () {
    $("#tb_birth").datepicker({
        dateFormat: "dd/mm/yy",
        changeYear: true,
        showOn: "button",
    }).next('button').text('').button({
        icons: {
            primary: 'ui-icon-calendar'
        },
        label: "选择日期",
        text: false
    }).on("change", function () {
        var isreadonly = getUrlParameter('readonly');
        if (isreadonly == "y") {
            $("#mybutton").attr("disabled", "disabled")
        }
        if (isreadonly == "y") {
            $("#tb_birth").datepicker("disable");
        }
    });
});

Open in new window

I now still cannot disable the option to choose the datepicker (when readonly=y). What to adjust?
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Very sorry, what is DOC READY BLOCK?
$(function() {
 // ANYTHING INSIDE HERE
})

Open in new window