Search code examples
jqueryjquery-uijquery-ui-resizable

Set another string if id/class does not exist in the options of resizable widget (jquery-ui)


I implemented a customized widget (alsoResizeReverse) based on followed reference: jQuery UI Resizable alsoResize reverse (I use the accepted answer)

However, I am wondering is there any solution to set another id if the id that specified on alsoResizeReverse does not exist?

For instance, here is my current code:

$("#select").resizable({
        handles: "s",
        alsoResizeReverse: "#content-timesheet",
        minHeight: 74.25,
        maxHeight: 378
    });

If "#content-timesheet" does not exist, I would like to set another id. Therefore, more or less, the logic would be like this:

if($('#content-timesheet').length){
    alsoResizeReverse = "#content-timesheet";
}else{
    alsoResizeReverse = "#content-useditems";
}

Thank you in advance.


Solution

  • You can check the length property of the jQuery object in a ternary expression. Try this:

    alsoResizeReverse: $('#content-timesheet').length ? '#content-timesheet' : '#content-useditems',
    

    Ternary reference at MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator