0

working with CKEDITOR and trying to disable the submit button if the length of the content is 0, but then on keyup, check the function again and see if the value is more than 1 and if so set the submit button to enabled.

So here we have this:

CKEDITOR.on('instanceReady', function(){
    disableNewPost();
    var e = CKEDITOR.instances['newpost'];
    e.on( 'keyup', function() {
        disableNewPost();
    });
});

function disableNewPost() {
    var value = CKEDITOR.instances['newpost'].getData();
    var valueL = value.length
    if ($(valueL) < 1) {
        $(".addnewpostbtn").prop("disabled", true);
    } else if ($(valueL) > 0) {
        $(".addnewpostbtn").prop("disabled", false);
    }
}

This gets the value and length correctly, and if put an alert(value) or alert(valueL) it displays what it shoudld.

However the if statement doesnt seem to work.

Onpage load, the CKEDITOR has no content, and an alert shows its length as 0.

However using the above if statement it doesnt disable the button. I have tried all variations i can think of ( == '' / == 0 ) etc. But no luck.

If the function just contains:

$(".addnewpostbtn").prop("disabled", true);

Then it does disable.

Any help?

4
  • Why would you use that vs. $(".addnewpostbtn").prop("disabled", $(valueL) < 1)? (But the real problem is that $(valueL) will always be truthy.) Commented Feb 12, 2014 at 22:14
  • $(".addnewpostbtn").prop("disabled", valueL > 0 && value L < 1); Commented Feb 12, 2014 at 22:16
  • if value.length is a Number, why on earth would you want to wrap it in jQuery $(valueL)? Commented Feb 12, 2014 at 22:17
  • Why are you creating valueL as value.length then passing it into jQuery again? you can just do valueL < 1. Alternatively, why don't you check to see if it's equal to 0 then have an else that handles everything else (if it's not 0, there can't be a negative length, unless using indexOf or some stuff like that) Commented Feb 12, 2014 at 22:18

1 Answer 1

5

You don't need to call $ to evaluate ordinary variables. It should be:

if (valueL < 1) {
    $(".addnewpostbtn").prop("disabled", true);
} else {
    $(".addnewpostbtn").prop("disabled", false);
}

or more simply:

$(".addnewpostbtn").prop("disabled", valueL === 0);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.