-7

I am having trouble in adding JavaScript variable in HTML tag as code is written below and i want reult as Hello world or u cansay that js var + html content

Html code :

<div class ="box" >user</div>

Javascript code :

 <script>
   var one = "Hello";
</script>

result should be like this =>Hello user

2
  • 3
    How could the result possibly be Hello user? Commented Jun 20, 2016 at 19:13
  • Strange that you feel "Hello" is the variable part of "Hello user". Commented Jun 20, 2016 at 19:14

3 Answers 3

2

You could create a <span> element and then use jQuery selector to set the html of that element.

Here is an example:

<div class = "box"><span class="span"></span>user</div>
    <script type="text\javascript">
        $(".box .span").html("Hello ");
    </script>
Sign up to request clarification or add additional context in comments.

1 Comment

You'll probably hast to explan him what is jQuery, or try another solution using vanilla javascript.
0

You can't do that with javascript tags. However, you can modify your html using the DOM. Start with this code snippet:

<div class="box" id="my-box">user</div>
<script>
  document.getElementById('my-box').innerHTML = "Hello " + document.getElementById('my-box').innerHTML;
</script>

Remember that script tag load and evaluate javascript code, but does not replace it in your html body.

Also, you should start programming javascript here.

Comments

0

This help you :

<!DOCTYPE html>
<html>
    <head>
    
    </head>
     <body>
         <div class ="box" >user</div>
         <p id="result"></p>
         <script>
             var box = document.getElementsByClassName("box")[0].innerHTML;
              document.getElementById("result").innerHTML = "Hello " + box;
         </script>
    </body>
</html>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.