0
<body>
     <script type="text/javascript">
         var a="sreedhar";
         <input type="text" value="abc">
     </script>
</body>

It gives the syntax error. can't we use "html tags" directly in "javascript".

2
  • 2
    You can't. Just put the input tag outside the <script> Commented Oct 11, 2013 at 12:12
  • 1
    My eyes! Aaaagh :) Just kidding. Between <script> and </script> the only thing allowed is the scripting language you are using. Could you provide a more accurate question? What do you want? Commented Oct 11, 2013 at 12:16

5 Answers 5

1

No, you can't use them directly in JavaScript.

However you may treat them as strings:

var str = '<input type="text" value="abc">';

or as DOM elements:

var input = document.createElement('input');
input.type = 'text';
input.value = 'abc';

And then append to the markup, e.g. document.body.appendChild(input);.

Sign up to request clarification or add additional context in comments.

Comments

0

Usually a well formated hmtl with javascript looks like this.

<HTML>
<HEAD>
        <script type="text/javascript">
              var a="sreedhar";
        </script>
</HEAD>    
<BODY>
        <input type="text" value="abc">
</BODY>
</HTML>

Comments

0

If you want to generate some html with javascript then assign it as a string to some variable.

For example you have a div having id='abc' and you want to generate a textbox in this div then you need to do like this

  <script type="text/javascript">
   var textbox = '<input type="text" value="abc">';
   $('#abc').append(textbox);
  </script>

Comments

0

Maybe you want this:

<script type="text/javascript">
var a="sreedhar";
document.write('<input type="text" value="abc">');
</script>
</BODY>

Comments

0
<html>
 <head>
  <title> New Document </title>
  <script type="text/javascript">
     //put javascript function here

  </script> 
 </head>

 <body>
     <input type="text" value="abc">
 </body>
</html>

You can use html element inside the javascript using element id.

Comments

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.