Solved

Find textbox inside formview in javascript

Posted on 2013-12-11
24
721 Views
Last Modified: 2013-12-12
Can someone help me out how to find a textbox and set the value in JavaScript.

I get it to work on IE 11.0 but it fails on IE 8

var textbox = document.getElementById('<%=formView1.FindControl("tbSomething").ClientID %>');
var comments = textbox.value;


On IE8 textbox is null here.....
0
Comment
Question by:pucko73
  • 15
  • 9
24 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
What do you get on the screen with the following :
window.onload = function() {
         var id = '<%=formView1.FindControl("tbSomething").ClientID %>';
         alert("the ID is :" + id); // for testing purpose
         var textbox = document.getElementById(id);
         alert("textbox " + (textbox==null)?"not found":"found" ); // for testing purpose
         var comments = textbox.value;
         alert(comments);
}

Open in new window

0
 

Author Comment

by:pucko73
Comment Utility
the id is ct100_ContentPlaceHolder1_formView1_tbSomething
not found
empty dialog
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
and on IE 11?
could you confirm when you see the first alert you can see the textbox in both cases?
0
 

Author Comment

by:pucko73
Comment Utility
I get the same result from both... It's just that I dont get an exception in the javacode for the script code on IE 11 it seems like.

On 11 i can se the textbox when I get the message with the id.  (but i still get not found)

on 8 I can see the textbox when the dialog with the id is open.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
ok, do a right to the page and choose view source on both.
try to locate the textbox and confirm the ID is the right one. Should not...
0
 

Author Comment

by:pucko73
Comment Utility
If i don't put this in the onoad function, just add the alert rows to my function that was called in a onclientclick of a button I get on 11:

id as above
not found
some text  (ie the content of textbox... so I get the value here......


and on 8 I get an exception:

0x800a138f - JavaScript runtime error: Unable to get property 'value' of undefined or null reference....
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
Validate your page here : http://validator.w3.org/
0
 

Author Comment

by:pucko73
Comment Utility
yes I can se the id  ctl00_ContentPlaceHolder1_formView1_tbSomething in view source on both.
0
 

Author Comment

by:pucko73
Comment Utility
Could it have something to do with it beeing a multiline textobox

I can se that view source show it as <textarea
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
the problem is when you try to read the data
you muts put the provided script alone in its own script tag (outside of any function) :
<script>
window.onload = function() {
         var id = '<%=formView1.FindControl("tbSomething").ClientID %>';
         alert("the ID is :" + id); // for testing purpose
         var textbox = document.getElementById(id);
         alert("textbox " + (textbox==null)?"not found":"found" ); // for testing purpose
         var comments = textbox.value;
         alert(comments);
}
</script>

Open in new window

0
 

Author Comment

by:pucko73
Comment Utility
Not much output from that.

Some warnings about:
The border attribute is obsolete. Consider specifying img { border: 0; } in CSS instead.

and

An img element must have an alt attribute, except under certain conditions. For details, consult guidance on providing text alternatives for images.

And:
 The cellpadding attribute on the table element is obsolete. Use CSS instead.
Should not affect this.
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
great. hope you got my previous comment too.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:pucko73
Comment Utility
I'm not sure I understand what you mean with your previous  comment?


The strange thing is when I debug it seems like the textbox is assigned  and I can see the properties of it   (running your onload function at least)
and it still say not found.....
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
any link to see your page?
0
 

Author Comment

by:pucko73
Comment Utility
Unfortunately not.  I don't have any public server to put on :(
0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
What about this (wait 10s before the alerts)  :

<script>
window.onload = function() {

setTimeout(function() {
         var id = '<%=formView1.FindControl("tbSomething").ClientID %>';
         alert("the ID is :" + id); // for testing purpose
         var textbox = document.getElementById(id);
         alert("textbox " + (textbox==null)?"not found":"found" ); // for testing purpose
         var comments = textbox.value;
         alert(comments);
},10000);

}
</script>

Open in new window

0
 

Author Comment

by:pucko73
Comment Utility
still got the same.

id correct
not found
empty
0
 

Author Comment

by:pucko73
Comment Utility
the not found thing is strange. because if there is something in the textbox
I get

id correct
not found
"content of textbox"
0
 

Author Comment

by:pucko73
Comment Utility
So i guess that it works with your code in onload.... but when I have it in my code
it fails.


I call a function on a button OnClientClick

and that function calls anoter function that try to access the textbox
0
 

Author Comment

by:pucko73
Comment Utility
I also notice that on ie8 when I press my button the page don't show the TextBox after I pressed the button....

The first function that I calll shows a dialog where I can select some values... And whan I Close that dialog the function that try to read from textbox are called.
0
 

Author Comment

by:pucko73
Comment Utility
I solved my problem by:

1. Change the function that I call in onClientClick on the button
to fetch the value from the combobox, then I pass that as a parameter to the second function I call.

So I guess that the problem is that the textbox in the page seems to disapear when I
show Another dialog from the javascript
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
Comment Utility
So?
0
 

Author Comment

by:pucko73
Comment Utility
I found the reason for the problem.
Seems like there is some bug with span tag in ie8.

In the dialog that I show I had:
<b>Some Text: </b><span id="myid" />

Open in new window


if I changed that to
<b>Some Text: </b><span id="myid"> </span>

Open in new window


The page behind the dialog that I show did not "disaperar" and I can find the textbox etc.




i
0
 

Author Closing Comment

by:pucko73
Comment Utility
Thanks for all your help.  I've learned a lot from you....
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

744 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

17 Experts available now in Live!

Get 1:1 Help Now