0

Can this code be written in a one-line format?

if ($('#sIsTopNav').text().trim() === 'True') {
    $('#chkIsTopNav').attr('checked','checked');
}else {
    $('#chkIsTopNav').removeAttr('checked');
}

TIA ..

1
  • 2
    Why would you want to do this? Don't fix what's not broken, also one liners don't always read well. Commented Dec 4, 2012 at 15:58

2 Answers 2

4

Try using conditional (ternary) operator,

$('#sIsTopNav').text().trim() === 'True' ? $('#chkIsTopNav').attr('checked','checked') :  $('#chkIsTopNav').removeAttr('checked');
Sign up to request clarification or add additional context in comments.

Comments

1

You can do:

$('#chkIsTopNav').prop('checked', $('#sIsTopNav').text().trim() === 'True')

Note:

  • Removing the checked attribute has the same effect as setting it to false.
  • Using .prop() is the current recommended way for setting properties like checked, disabled etc.

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.