Javascript error

Posted on 2012-03-22
Last Modified: 2012-04-10
Hello Experts,

I have a javascript function which is called Client-Side on button Click event. Where I'm passing arguments as ClientID.
 function Validate(id)
       var txtData = document.getElementById(id);

on button:

<asp:Button ID="btnload" Text="Upload" runat="Server" Width="70px" Height="25px"
                                                            OnClientClick="Validate('<%= fuInfo.ClientID %>')" />

Open in new window

When I try to access variable txtdata, I get nothing or BLANK. For testing purpose I checked the parameter value of ID, its has data(using alert statement), but when I do document.getElementById, then no result.

If I do this, it works:
var txtData = document.getElementById('<%= fu.ClientID %>');

Open in new window

Question by:ASPDEV
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 29

Expert Comment

by:Paul Jackson
ID: 37753790
In your onclientclick you are passing in the ClientID of fuInfo :

OnClientClick="Validate('<%= fuInfo.ClientID %>')"

, in the code you say that works you are using fu.ClientID.

maybe your OnClientClick should be : OnClientClick="Validate('<%= fu.ClientID %>')"
LVL 82

Expert Comment

ID: 37753791
what about :
<script language="javascript" type="text/javascript">
var fuInfo = '<%= fuInfo.ClientID %>';
<asp:Button ID="btnload" Text="Upload" runat="Server" Width="70px" Height="25px"
                                                            OnClientClick="Validate(fuInfo)" />

Open in new window

LVL 38

Expert Comment

by:Tom Beck
ID: 37753828
What about in code behind?

btnLoad.Attributes.Add("onclick", "Validate(\"" + fuInfo.ClientID + "\")");
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Expert Comment

by:Lalit Chandra
ID: 37753839
i think you need to replace your button decleration to

<asp:Button ID="btnload" Text="Upload" runat="Server" Width="70px" Height="25px"
                                                            OnClientClick="Validate('<%= fu..ClientID %>')" />
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 37753869
var txtData = document.getElementById(id);  gets the 'object' without any of the details.  You have to use one of the properties of the object to display or do anything.  Like this example shows the 'innerHTML'.  For <input> tags, you usually use the 'value'.

<script type="text/javascript">
function getValue()
var x=document.getElementById("myHeader");
<p id='myHeader'>This is the innerHTML.</p>

Open in new window


Author Comment

ID: 37754224
Sorry, but none of them worked.

The reason for client ID of each control because I have 3 buttons and 3 Fileupload buttons.

On each button click, I want to make use of same function Validate(), so that I can pass each clientId of Fileupload button.

If I write  different function(does same logic) for each button, then it works!

Accepted Solution

Lalit Chandra earned 250 total points
ID: 37754320
why don't you try jquery for that. you can simple code the following
$(function() {
   $("#button1").click(function  {
       var str = $("#fileUpload1").val();
   $("#button2").click(function  {
       var str = $("#fileUpload2").val();
   $("#button3").click(function  {
       var str = $("#fileUpload3").val();


Hope this will resolve your problem.

Expert Comment

by:Vishal Kedar
ID: 37770201
try below js function instead of yours

function Validate(id)
          // var txtData = document.getElementById(id);


may it helps

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

623 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