Solved

Missing getElementsByTagName.

Posted on 2007-12-07
20
1,236 Views
Last Modified: 2012-05-05
Hi,

My code are below in aspx:

<script type="text/javascript">

{        
           var control = document.getElementById("textbox1");
         
        }

 </script>

My questionis why I can not get "getElementsByTagName" property when I code document?

It just have "getElementById" and "getElementByName" Property.


Thank you.
0
Comment
Question by:emi_sastra
  • 9
  • 7
  • 4
20 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426631
It should be
var control = document.getElementById("input");

as its TAG name like <input
 
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426634
What are you basically looking for?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20426661
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Author Comment

by:emi_sastra
ID: 20426672
Hi Asvforce,

Why I just have two properties 'getElementById" and "getElementByName"?
In this case I can not use document.getElementsByTagName.

Thank you.


0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20426683
you can still use getElementsByTagName  , even its not showing up
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426699
see sample

<input type="text" name="t1" id="t2">some text</input>

for this  you can either use this

document.getElementById("t2").value
or
document.getElementByName("t1").value

Note: It all depends on what property you have in your html tag, normally you will see name property so just use getElementByName
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20426717
Here are my more code:

 <script type="text/javascript" >
   
    function Set_Control()
        {        
           var control = document.getElementsByTagName("asp:TextBox");
           
           alert( control.length)
         
               for ( var i = 0; i < control.length; i++) {
               alert('Hello')
           }
        }
   
   
    </script>

<body onload="Set_Control()">
   
    <form id="form1" runat="server">
   
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div>
           
    </form>
</body>

It just show 0.

Thank you.
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20426742
Put the script inside for tag
 
 
<body onload="Set_Control()">
    
    <form id="form1" runat="server">
    
        <asp:ScriptManager ID="ScriptManager1" runat="server" />
        <div>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            </div> 
            <script type="text/javascript" >
    
    function Set_Control()
        {        
           var control = document.getElementsByTagName("asp:TextBox");
           
           alert( control.length)
          
               for ( var i = 0; i < control.length; i++) {
               alert('Hello') 
           }
        }
    
    
    </script>
    </form>
</body>

Open in new window

0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 20426750
Actually I think its working for HTML tags only
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426752
So you want to find all text box controls? If yes then use this
 <script type="text/javascript" >

function Set_Control()
{        
           var control = document.getElementsByTagName("input");
           alert( control.length);
               for ( var i = 0; i < control.length; i++) {
                         if(control[i].type == "text")
                             alert('Hello' + control.name);
           }
}
</script>
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20426794
Hi asvforce,

It shows nothing. Since there is no "input" tag there.

Thank you.
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426817
It will show you input tag when the html file is created, not while coding. So the <asp:TextBox will change to input tag like <input type="text". So just view the source code of you HTML file which is being shown and you will see <input type="TEXT" something instead of <asp:TextBox. Also try using this script

function Set_Control()
{        
           var control = document.getElementsByTagName("input");
           alert( control.length);
               for ( var i = 0; i < control.length; i++) {
                         if(control[i].type.toLowerCase() == "text")
                             alert('Hello' + control.name);
           }
}
</script>
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20426835
Hi asvforce,

This time it show 6 and "Helloendefine"

Thank you.
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426848
Good, so now you are good. see the script where i have written Hello' + control.name so endefine is the name, and in all total there are 6 text boxes on your html page. I guess from here you have to carry forward with what you want to do. Or else let me know what exactly you want to do and /or looking to do.
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426859
Sorry there are 6 <input tags and the Helloendefine is the name which i was trying to display so better use this to view what text boxes are comming
function Set_Control()
{        
           var control = document.getElementsByTagName("input");
           alert( control.length);
               for ( var i = 0; i < control.length; i++) {
                         if(control[i].type.toLowerCase() == "text")
                             alert('Hello: ' + control.id);
           }
}
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20426874
Hi asvforce,

Still show 6 and "Hello : undefined"

Thank you.
0
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20426882
Do one thing, when you see the html file or the web page, please copy the source and paste it here.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20426884
Hi asvforce,

Still show 6 and "Hello : undefined"

This code alert('Hello: ' + control.id); should be changed to:alert('Hello: ' + control[i].id)

And it works.

Thank you.
0
 
LVL 23

Accepted Solution

by:
Ashish Patel earned 500 total points
ID: 20426898
oh sorry for that mistake, Good to hear that it is working now for you. I hope from here you can continue working forward and that you problem is solved. I guess you now understand that javascript understands html objects and tags and not server side asp.net tags.
0
 
LVL 1

Author Comment

by:emi_sastra
ID: 20426932
Yes, my problem is solved.

Thank you very much.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now