Solved

Javascript error

Posted on 2012-03-22
8
360 Views
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

0
Comment
Question by:ASPDEV
8 Comments
 
LVL 29

Expert Comment

by:Paul Jackson
Comment Utility
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 %>')"
0
 
LVL 82

Expert Comment

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

Open in new window

0
 
LVL 38

Expert Comment

by:Tom Beck
Comment Utility
What about in code behind?

btnLoad.Attributes.Add("onclick", "Validate(\"" + fuInfo.ClientID + "\")");
0
 
LVL 7

Expert Comment

by:Lalit Chandra
Comment Utility
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 %>')" />
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 82

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
Comment Utility
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");
alert(x.innerHTML);
}
</script>
<p id='myHeader'>This is the innerHTML.</p>

Open in new window

0
 

Author Comment

by:ASPDEV
Comment Utility
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!
0
 
LVL 7

Accepted Solution

by:
Lalit Chandra earned 250 total points
Comment Utility
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.
0
 
LVL 5

Expert Comment

by:Vishal Kedar
Comment Utility
try below js function instead of yours

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

      }

may it helps
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to define a gradient in CSS. Create a new HTML document with an internal stylesheet.: Create a div in CSS and name it Gradient. Define the background as "linear-gradient(to right, #ee3668, black)". Ensure you …
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…

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

10 Experts available now in Live!

Get 1:1 Help Now