-3

I want to embed a JavaScript if clause into a HTML code.There are two buttons named success and danger those are separated by a if clause. I tried for it as following manner.

<script>
    if (1 < 18) {
</script>
       <button type="button" class="btn btn-success btn-rounded">Success</button>
<script>
    }
    else{
</script>
       <button type="button" class="btn btn-danger btn-rounded">Danger</button>
<script>
    }
</script>

But it did not work in properly. Then what should I do for it.

ruwan liyanage
  • 419
  • 2
  • 11
  • 24
  • Possible duplicate of https://stackoverflow.com/questions/44109178/how-to-append-div-in-angular-js-depending-upon-condition-angular-js – Anis R. May 03 '19 at 18:20
  • Actually a duplicate of https://stackoverflow.com/q/35163009/215552, which is for Angular, not AngularJS. – Heretic Monkey May 03 '19 at 18:24
  • 1
    Are you actually doing this in Angular? You used the tag, but there isn't any Angular-specific code in your example, and it generally doesn't use ` – John Montgomery May 03 '19 at 18:27
  • @John Montgomery Yes I'm using angular. This is the `component.html` file. How I use a if clause for separate those buttons – ruwan liyanage May 03 '19 at 18:30
  • Use `*ngIf` instead. – The Head Rush May 03 '19 at 18:36
  • Just based on this question, I would suggest that you read the tutorial, using `*ngIf` is such a basic thing to know, so to get the most out of angular I **strongly** suggest you read the tutorial: https://angular.io/tutorial – AT82 May 03 '19 at 20:07

2 Answers2

3

There's a few different ways you can do this in Angular. The simplest change would be to just use *ngIf:

<button *ngIf="1 < 18" type="button" class="btn btn-success btn-rounded">Success</button>
<button *ngIf="1 >= 18" type="button" class="btn btn-danger btn-rounded">Danger</button>
John Montgomery
  • 6,739
  • 9
  • 52
  • 68
0

You'll need to write the HTML code with the JavaScript, see below.

<script type="text/javascript">
var i = 0;
window.onload = function(){
if (i == 0)
{
     document.getElementById('somearea').innerHTML = '<input type="button" value="Some Value 1"/>';
}
else
{
     document.getElementById('somearea').innerHTML = '<input type="button" value="Some Value 2"/>';
}
}
    </script>
sys_adm_dev
  • 65
  • 1
  • 8