473,412 Members | 4,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

How to use the same javascript function for different controls in content page, vb.ne

Hi,

I'm having a javascript code which takes the field/control id and checks whether it contain alphabets or not. This javascript code can be reused for many other similar fields.
So i'm passing the "id" of the control while calling the javascript function. This was working fine too.
Now when i added a master page to the web page, the ID of the control in the content page will get appended with a clientID. As because of this when i pass the control ID while calling the script function, unable to find the control as it is appended with client ID.
I dont want to repeat the .js code for each control separately, which has the same functionality.

If i directly give the field name in the .js then its working fine.
Expand|Select|Wrap|Line Numbers
  1. <script type="javascript">
  2. function checkAlpha(FieldID)
  3. {
  4. var txt = document.getElementById('<%=txtUserName.ClientID%>').value;
  5. alert(txt)
  6. }
  7. </script>
If i directly pass, txtUserName which is ID, then it works fine. But while calling this function, passing a param which holds the ID, which will vary for different conrols is not working. ClientID will be appended to control.
How to append this FieldID in javascript, Please help me.
Thanx in Advance,
Vigneshwarie
Jun 27 '11 #1

✓ answered by Frinavale

Couldn't you just pass the whole element to the control instead of the ID?

Like so:
Expand|Select|Wrap|Line Numbers
  1. <input type="Text" onkeyup="return checkAlpha(this);" />
  2.  
Or in ASP.NET it would look more like:
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="myTextBox" onkeyup="return checkAlpha(this);" />

Now in your script you don't need to use document.getElementById since you've passed the element into the method...

Expand|Select|Wrap|Line Numbers
  1. <script type="javascript">
  2. function checkAlpha(txt)
  3. {
  4.   alert(txt.value)
  5. }
  6. </script>
-Frinny

2 3521
Frinavale
9,735 Expert Mod 8TB
Couldn't you just pass the whole element to the control instead of the ID?

Like so:
Expand|Select|Wrap|Line Numbers
  1. <input type="Text" onkeyup="return checkAlpha(this);" />
  2.  
Or in ASP.NET it would look more like:
Expand|Select|Wrap|Line Numbers
  1. <asp:TextBox ID="myTextBox" onkeyup="return checkAlpha(this);" />

Now in your script you don't need to use document.getElementById since you've passed the element into the method...

Expand|Select|Wrap|Line Numbers
  1. <script type="javascript">
  2. function checkAlpha(txt)
  3. {
  4.   alert(txt.value)
  5. }
  6. </script>
-Frinny
Jun 28 '11 #2
Oh! Thanx Frinny.
This "this" didnt strike me.
Thank you so much.
Vigneshwari
Jun 29 '11 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Cristian | last post by:
Hi all, When I download a file with ftp_nb_get, I'd like to show the elapsed time and so I had insert this javascript function in my php page <SCRIPT> function scrivi(num, num2, num3) {...
1
by: BillG | last post by:
In my DeleteButton_OnDelete method I would like to get confirmation from the user as to whether they are sure they want to delete a member. How do I call a js routine to confirm the choice?
2
by: Shikari Shambu | last post by:
Hi, I have an ASP.NET website that has pages that inherit from a BasePage. I want all pages to have some common Javascript functions. I tried doing the following In the Base Page protected...
1
by: jeff_m | last post by:
Hello all, I'm having an issue with the following: I have an .aspx page with two html links that call distinct Javascript functions. I also have an iframe in the html of this .aspx page....
2
by: weird0 | last post by:
How can i call a javascript from a asp.net content page on the clicking of a button. Where the Hell should I place the code which should be, I guess: <script> function Open() {...
2
by: sudhashekhar30 | last post by:
hi i have done dis b4.but now it's not working.don't no why?can any one help why it's so? javascript code is in content page like dis--- javascript in html page:- <script...
3
by: Hanoodah | last post by:
Good morning all, Please I need help in a form that I have designed to fill user information. This form needs two drop-down list about specific date, one about contract date, and the other about...
2
by: Andrew Jocelyn | last post by:
Hi Maybe it's because it's Friday afternoon but I can't work out how to simply use the same Label control in two different PlaceHolder controls. This is what happens. When I add the Label to the...
9
by: btreddy | last post by:
Hii all, I've a a javascriptfunction in my master page JavaScript Function: var meetingID=null; var panelID=null; var fileGridID=null; function...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.