Solved

Find textbox inside formview in javascript

Posted on 2013-12-11
24
727 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
ID: 39711431
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
ID: 39711469
the id is ct100_ContentPlaceHolder1_formView1_tbSomething
not found
empty dialog
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39711474
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
ID: 39711501
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
ID: 39711522
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
ID: 39711536
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
ID: 39711542
Validate your page here : http://validator.w3.org/
0
 

Author Comment

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

Author Comment

by:pucko73
ID: 39711555
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
ID: 39711575
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
ID: 39711590
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
ID: 39711604
great. hope you got my previous comment too.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:pucko73
ID: 39711633
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
ID: 39711658
any link to see your page?
0
 

Author Comment

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

Expert Comment

by:leakim971
ID: 39711727
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
ID: 39712062
still got the same.

id correct
not found
empty
0
 

Author Comment

by:pucko73
ID: 39712066
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
ID: 39712081
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
ID: 39712105
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
ID: 39712263
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
ID: 39712334
So?
0
 

Author Comment

by:pucko73
ID: 39713423
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
ID: 39713433
Thanks for all your help.  I've learned a lot from you....
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

930 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