0

I have this line of Javascript:

if (true){ 
  $('#custom-tag').html('HTML');
} 
else {
  $('#custom-tag').html('DifferentHTML');
}

If a variable is yes, it will run this html code at startup. If the variable is not yes, then it will run this line of html at startup. How can I do this?

EDIT: I'm afraid I forgot to say, I was wanting this to detect your browser. If using chrome, it will put out no HTML. If using other browsers than chrome, it will put out HTML.

EDIT#2: I have this code. I need to fix this for the popup that identifies if it's chrome.

   $(function() {

     $('#in').on('keyup', function() {
       validate();
     });

   });

   function validate() {
     var x = $('#in').val();

     if (navigator.userAgent.indexOf("Chrome") != -1) {
       $('#result').html('<h3>Chrome</h3>');
     } else {
       $('#result').html('');
     }

   }
<input type="text" id="in" onChange="validate()" />

<div id="result">





  <div id="popup" class="overlay">
    <div class="popup">
      <h2>Browser</h2>
      <a class="close" href="javascript:popupClose();">×</a>
      <div class="content">
      </div>
    </div>
  </div>

Just need to connect all of these. EDIT: I need to add the last line of html into the java code if it's not chrome.

Akidus
  • 191
  • 11

5 Answers5

1

Try the one line snippet which use ternary operator.

// Check if not chrome
if(!$.browser.chrome){
    // If its variable.
    $('#custom-tag').html( (variable == 'yes') ? 'html' : 'different html' );
}

// If its checkbox.
$('#custom-tag').html( $('#checkboxId').is(":checked") ? 'html' : 'different html' );
Venkat.R
  • 7,420
  • 5
  • 42
  • 63
1

Here it works

var name;
if(name.checked == true){
 $('#custom-tag').html('HTML');
}else{
 $('#custom-tag').html('DifferentHTML'); 
}
Wasiq Muhammad
  • 3,080
  • 3
  • 16
  • 29
0
var x = true;  // Your dynamic result True or False is stored in x

if(x)
{
    $('#selector').html();
}
else
{
    $('#selector').html();
}

You can also do it by defining var x = ''; in the beginning of script and then depending upon your other script statements, it would get value either true or false. Then you need to check x = '' or not. That's it!

Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

$(function(){

  $('#in').on('keyup',function(){
    validate();
  });

});

function validate(){
    var x = $('#in').val();
    
  if(x  == "x"){
    $('#result').html('<h3>x entered</h3>');
  }else{
    $('#result').html('<h5>something other than x entered</h5>');
  }

}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="in" onChange="validate()"/>

<div id="result">
  
  
</div>
Esteban Rincon
  • 2,040
  • 3
  • 27
  • 44
0

You can check if its chrome this way:

if( navigator.userAgent.toLowerCase().indexOf('chrome') > -1 ){ 
  // chrome, do nothing
} 
else {
  // not chrome put html
  $('#custom-tag').html('HTML');
}