-1

i have crated a text-box, which has an id, i want to know, is it possible to find out control name using getElementbyId.

<!doctype html>
<html lang="en">
    <body>
        <script type="text/javascript" language="javascript">
            var a = document.getElementById('first').type;
            document.write(a);
        </script>
        <input type="text" id="first"></input>
    </body>
</html>
Gene R
  • 3,684
  • 2
  • 17
  • 27
Abhishek Kumar
  • 51
  • 1
  • 10

2 Answers2

1

is it possible to find out control name using getElementbyId.

You need to getAttribute of the element and

var element = document.getElementById('first');
alert(element.getAttribute("type")); //alerts its type attribute
alert(element.getAttribute("name")); //alerts its name attribute
alert(element.nodeName); //alerts its name 

full example

<!doctype html>
<html lang="en">
<body>

<input type="text"  id="first" name="firstName">

 <script>
   var element = document.getElementById('first');
   alert(element.getAttribute("type"));
   alert(element.getAttribute("name"));
</script>


</body>
</html>

Observe that script tag is after the text input tag since unless the tag is loaded onto the DOM, DOM API (document.getElementById) will not be able to pick it up.

gurvinder372
  • 66,980
  • 10
  • 72
  • 94
-1

i made this fiddle as example: https://jsfiddle.net/z0vdhp1m/

few posibble problems: input tag is not paired

    <input .... />

it's more clear to acces via getAttribute('type') than .type, but should work anyway.

document.write doesn't work on jsfiddle so i didn't test that, but if you were just to print it, then i suggest:

          alert();

or

          console.log()
Jimmmy
  • 579
  • 12
  • 26