How to set the value of an element using jquery and conditionally set a manual value if the value is empty

0

I have a form, which when submitted, sends data to a modal window. I have a jQuery plugin that has a jQuery plugin. I have a jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery jQuery

$ ("# diagnostic_model"). text ($ ("#diagnostic"). val ());

I need to pass a fixed text if the value of the "diagnostic" texbox is empty (something like "Not informed"), and unfortunately I do not know how to do that. Can you help me?

    
asked by anonymous 11.03.2016 / 01:52

1 answer

1

I'll show you two techniques (inumeras) to do this, a traditional one with if and else

var texto = $("#diagnostico").val();
if(texto != "")
   $("#modal_diagnostico").text( texto );
else
   $("#modal_diagnostico").text( "Texto estático" );

Now ternary operators (which basically does the same thing in this example)

$("#modal_diagnostico").text( ($("#diagnostico").val()) ? $("#diagnostico").val() : "Texto estático" );
    
11.03.2016 / 03:35