0

I am trying to find a away (the correct and easy way) to use IF and else IF statements in javascript.

here is what I am trying to do...

at the moment, I am joining 2 input fields value and display them in another input balue like so:

function FillIn() {
  document.getElementById('custom').value = 

'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' + '1st Text:' + ' ' + document.getElementById('inputName101').value

    ; 
}

so basically the above code will place the values of inputName and inputName101 into the custom input field. this works just fine.

Now, I have 2 radio buttons which I want to display their Values into the custom input field depending on which one is selected/checked.

for this I am using the following code:

    function FillIn() {
      document.getElementById('custom').value = 

    'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' + '1st Text:' + ' ' + document.getElementById('inputName101').value

if(document.getElementById('male').checked) {

   + ', ' + 'Type:' + ' ' + document.getElementById('male').value

}else if(document.getElementById('female').checked) {

     + ', ' + 'Type:' + ' ' + document.getElementById('female').value

}

        ; 
    }

however, i am not sure if I am doing this correctly as I don't get the values of radio buttons in the custom input field at all..

Could someone please advise on this?

EDIT:

HTML for radio buttons:

<input id="male" type="radio" name="gender" value="Road Legal" onclick="showhidediv(this);">  

<input id="female" type="radio" checked="checked" name="gender" value="Show Plate" onclick="showhidediv(this);">
2
  • can you share your HTML code for radio buttons? Commented Mar 24, 2014 at 13:34
  • Where are you calling fillin? And why is female "show plate" and male "road legal"? Commented Mar 24, 2014 at 13:59

3 Answers 3

2

Actually, you're doing it wrong, you can't directly concatenate if / else conditions with strings, and the cleaner way to write your code would be

function FillIn() {
    var str = 'Main Text: ';
    str += document.getElementById('inputName').value;
    str += ', 1st Text: ';
    str += document.getElementById('inputName101').value;

    if ( document.getElementById('male').checked ) {
        str += ', Type: ';
        str += document.getElementById('male').value;
    } else if ( document.getElementById('female').checked ) {
        str += ', Type: ';
        str += document.getElementById('female').value;
    }

    document.getElementById('custom').value = str;
}
Sign up to request clarification or add additional context in comments.

2 Comments

var gender = document.getElementsByName("gender"); str += ', Type: '+(gender[0].checked)?gender[0].value:gender[1].value;
@mplungjan - Indeed, a ternary in parenthesis can be concantenated, I did do that at first, but decided to write it really verbose to make a point; you don't have to make a mess.
0

That won't work as it is... you can't just concatenate strings in and out of if statements as if it was a string. You need to do it like this...

function FillIn() {
  document.getElementById('custom').value = 'Main Text:' + ' ' +  
    document.getElementById('inputName').value + ', ' + '1st Text:' + ' ' + 
    document.getElementById('inputName101').value;

  if (document.getElementById('male').checked) {
    document.getElementById('custom').value += ', ' + 'Type:' + ' ' +
      document.getElementById('male').value;
  }
  else if (document.getElementById('female').checked) {
    document.getElementById('custom').value += ', ' + 'Type:' + ' ' + 
      document.getElementById('female').value;
  }
}

In fact, that most probably threw an error on console, you should check those to see what's wrong with your code.

5 Comments

Agree, but the OP clearly lacks the knowledge to fully understand how this works. So making it elegant deffinitely has no use for him. Other than that, I assume you down-voted, do you actually think the answer is not useful? One thing is for it not be elegant, but it does work and it did answer the OP's question.
elegant or inelegant.. it answered my question so appreciate your help. I don't know why I have to write a code that is pretty or elegant as some call it when either way it works the same!
Because with that attitude you are certain to paint yourself into a corner with inelegant code that does not work for some reason
@mplungjan, what you just said holds no water. why? well, because the "inelegant" code as you called it works very well and it is fairly explanatory... if it works now then I don't see why it should just suddenly stop working in the future.
I re-formatted so it is less horrific and removed my downvote. I still strongly dislike the repeated code instead of using a var and the lack of ternary which is not rocket science. Let's agree to disagree and you will mark my words one day
0

You can't use an if statement inside an expression. You have happened to write it in a way that actually runs, but the expressions inside the if statement are just evaluated and discarded.

You can use the conditional operator in an expression:

function FillIn() {
  document.getElementById('custom').value = 
    'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' +
    '1st Text:' + ' ' + document.getElementById('inputName101').value +
    (document.getElementById('male').checked ?
      ', ' + 'Type:' + ' ' + document.getElementById('male').value :
      (document.getElementById('female').checked ?
        ', ' + 'Type:' + ' ' + document.getElementById('female').value :
        ''
      )
    ); 
}

Using local variables for the elements makes the code a bit more readable:

function FillIn() {
  var male = document.getElementById('male');
  var female = document.getElementById('female');
  document.getElementById('custom').value = 
    'Main Text:' + ' ' +  document.getElementById('inputName').value + ', ' +
    '1st Text:' + ' ' + document.getElementById('inputName101').value +
    (male.checked ?
      ', ' + 'Type:' + ' ' + male.value :
      (female.checked ?
        ', ' + 'Type:' + ' ' + female.value :
        ''
      )
    ); 
}

1 Comment

Why not assign fillin to onclick and use "type "+ this.value?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.