Javascript use if statement in variable

Truzze

Hello stackoverflow community,

I'm trying to create a variable through an if statement and other variables but it doesn't work. If anyone could help me i would really appreciate it.

This is my code to create the returntouser variable

function userinput() {
  return returntouser = metaTitle + "\n" +  metaDescription + "\n" + metaKeywords;
}

Now i'm trying to make an if function out of the "metaTitle" variable. If an user don't write anyting in the textform, the "metaTitle" variable will not be added to the "returntouser" variable.

function userinput() {
  return returntouser = if (document.getElementById("userinputTitle").length > 0) { return metaTitle } + "\n" +  metaDescription + "\n" + metaKeywords;
}

Can someone tell me how i can add this if function to either add the "metaTitle" variable to the "returntouser" variable or not

Daniel A. White

You have to do it in separate steps.

function userinput() {
  var returntouser = "";
  if (document.getElementById("userinputTitle").length > 0) { returntouser += metaTitle } 
  return returntouser + "\n" + metaDescription + "\n" + metaKeywords;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Use of javascript variable in php if statement

How to use javascript variable in django if statement (template)?

Trying to use javascript variable in an append statement for an a href

JavaScript declare a variable and use the comma operator in one statement?

Adding <a> element to an existing Javascript variable for use in .html statement

regex - Unable to use regex variable in conditional statement (JavaScript)

Use a variable in Objectname for WITH statement

javascript variable scope in the IF statement

Variable inside if statement in javaScript

returning a variable of an if statement javascript

Javascript: using a for statement as a variable

JavaScript IF statement not recognizing variable

How I can use a django variable in if statement of django template into a javascript string variable

PHP - Declare variable in if statement and use it in the same statement

mysql USE CASE STATEMENT as variable

use variable in if statement operator in ruby

Use variable in sqlalchemy ilike statement

Use a variable in a postgresql execute statement

How to use a variable in switch statement

R - use if statement to regroup variable

Use @ symbol as variable in if statement in Excel

Declaring a variable inside if statement in JavaScript

If statement saved inside Javascript variable

Variable loses value on if statement in javascript

if statement counter variable confusion javascript

Javascript changing variable with if statement in forEach

How to use JavaScript variable outside if-statement scope between two if-statements when it is declared in one of them?

How to use OR condition in a JavaScript IF statement?

illegal use of break statement; javascript