You can use a ternary statement (that's the fancy name for the if on one line trick):
var is_ok = true;
var myMsg = is_ok ? "You're good to go" : "Error";
But you are changing two parts of your p tag so you might want to do something like this instead:
// defaults
var myMsg = "You're good to go";
var msgClass = "ok-box";
if ( !is_ok ) {
myMsg = "Error";
msgClass = "not-ok-box";
}
Trouble with that is you now have two variable flying around...not very tidy so instead you can wrap them up in an object:
var myMsg = {
text : "You're good to go",
cssClass : "ok-box"
}
if ( !is_ok ) {
myMsg.text = "Error";
myMsg.cssClass = "not-ok-box";
}
which is neater and all self contained.
var myBox = '<p class="' + msgClass + '">' + myMsg + '</p>';
However I'd create the element using code (which I don't know how to do in jquery as I'm a mootools boy). In mootools it would be something like this:
myBox = new Element( "p", { class : msgClass, html : myMsg } );