Baseer Ul Hassan
Baseer Ul Hassan

Reputation: 39

Javascript inline HTML

I need to write HTML and Javascript code inline i.e. inside HTML Body (Need to display some random whole number value) I sought a lot of blogs but found no help so far in doing so. Please advise.

I wanna achieve this functionality:

   <td class="vhead">Offered Calls</td>
   <td>
      <script>
       Math.random();
      </script>
  </td>
  </td>

Upvotes: 1

Views: 2450

Answers (3)

user4602228
user4602228

Reputation:

for the most simple case

<td class="vhead">Offered Calls</td>
<td>
   <script>
      document.write(Math.random());
   </script>
</td>

Upvotes: 0

dmoo
dmoo

Reputation: 1529

Javascript does not work like this. It can with the help of templating libraries. Instead you will need to get a reference to the place in html you want to inject this random value. The below assumes you will have many and want a different random number in each.

<td class="vhead">Offered Calls</td>
<td class="random"></td>

<script>
  window.document.onload = function(){
     var elements = document.getElementsByClassName('random');
     [].forEach.call(elements, function (el) { el.innerHTML( Math.random() ) });
  }
</script>

Upvotes: -1

Osoter
Osoter

Reputation: 571

Try this

<td id="demo"></td>

<script>

    document.getElementById("demo").innerHTML = Math.random();

</script>

Upvotes: 4

Related Questions