inline if else statements in jQuery

javascript, jquery

Solution

you have a syntax error

if div = "choice1" 

should be

if (div == "choice1")

Anyway, the pattern you're looking for is:

div == "choice1" ? <code for true> : <code for false>

Problem

I have a jQuery function that is executed by two different buttons. ``` $("#btnSearch, #btnDirectorSearch").click(function () { ``` Part of the html that this function builds depends on which button was hit. I am using data- attributes to store my variables like this: ``` var div = $(this).data("str"); ``` And the html string I am building depends on what value the variable "div" is. Is there a way to do an inline if/else statement in jQuery? ``` if div = "choice1" { html += '<tr data-str = "str1" data-dataItem = "dataItem1" data-result-title = "' + name + '" data-result-id="' + sel + '">'; } else { html += '<tr data-str = "str2" data-dataItem = "dataItem2" data-result-title = "' + name + '" data-result-id="' + sel + '">'; } ``` That seems cumbersome and I'm hoping there is a better jQuery way of doing this. Thanks!

Original source

Related problems