473,398 Members | 2,525 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,398 software developers and data experts.

How to call javascript function from different<div> ?

I`v made the following code:

Expand|Select|Wrap|Line Numbers
  1. <div id="Panels" style="width: 28%;">
  2.             <asp:Panel ID="one" runat="server" CssClass="Panel">
  3.                 <div id="Head" onclick="doToggle();">
  4.                     HEADER
  5.                 </div>
  6.             </asp:Panel>
  7.             <asp:Panel ID="two" runat="server" CssClass="Content">
  8.                     abcdefghijklmnopqrstuvwxyz
  9.            </asp:Panel>
  10.             <cc1:CollapsiblePanelExtender ID="cpe" runat="server"  TargetControlID="two"></cc1:CollapsiblePanelExtender>
  11. </div>
  12.  

and the function doToggle() is:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2.         function doToggle()
  3.         {
  4.             var cpe = $find("cpe");
  5.             //cpe._toggle();
  6.             if (cpe.get_Collapsed()) 
  7.             {   
  8.                 cpe._doOpen();
  9.             } 
  10.             else
  11.             {
  12.                 cpe._doClose();
  13.             }
  14.         }
  15.     </script>


But the problem is the if i want to have more div tags but use this same function for all just by calling it by giving the appropriate id?
Oct 1 '08 #1
9 20154
acoder
16,027 Expert Mod 8TB
Yes, just pass the ID as a parameter to the function:
Expand|Select|Wrap|Line Numbers
  1. function doToggle(id) {
  2.             var cpe = $find(id);
PS. please use code tags when posting code.
Oct 1 '08 #2
but how will i call it?
plz tell me this as well
Oct 1 '08 #3
acoder
16,027 Expert Mod 8TB
Pass the ID to the function when calling:
Expand|Select|Wrap|Line Numbers
  1. onclick="doToggle('cpe')"
Oct 1 '08 #4
I`v made the following code:

Expand|Select|Wrap|Line Numbers
  1. <div id="Panels" style="width: 28%;"> 
  2.             <asp:Panel ID="one" runat="server" CssClass="Panel"> 
  3.                 <div id="Head" onclick="doToggle();"> 
  4.                     HEADER 
  5.                 </div> 
  6.             </asp:Panel> 
  7.             <asp:Panel ID="two" runat="server" CssClass="Content"> 
  8.                     abcdefghijklmnopqrstuvwxyz 
  9.            </asp:Panel> 
  10.             <cc1:CollapsiblePanelExtender ID="cpe" runat="server"  TargetControlID="two"></cc1:CollapsiblePanelExtender> 
  11. </div> 


and the function doToggle() is:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript"> 
  2.         function doToggle() 
  3.         { 
  4.             var cpe = $find("cpe"); 
  5.  
  6.             if (cpe.get_Collapsed())  
  7.             {    
  8.                 cpe._doOpen(); 
  9.             }  
  10.             else 
  11.             { 
  12.                 cpe._doClose(); 
  13.             } 
  14.         } 
  15.     </script> 
Actually i want to use single 'CollapsiblePanelExtender' for many <div> tags, that is i just need to specify in function the targetId of div tags to act on everytime from where the function is called....
Oct 2 '08 #5
Dormilich
8,658 Expert Mod 8TB
if you use element.addEventListener() / element.attachEvent() you can refer to the element's id with this.id

regards
Oct 2 '08 #6
gits
5,390 Expert Mod 4TB
threads merged - please don't double post threads and keep all questions regarding one issue in one thread.

kind regards
MOD
Oct 2 '08 #7
acoder
16,027 Expert Mod 8TB
If the previous answers don't help solve the problem, post the client-side version of your code, i.e. how it appears in the browser (view source).

PS. please use code tags when posting code.

PPS. don't use the report link when you only mean to reply. Reply is on the right.
Oct 2 '08 #8
[HTML]<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Panel.aspx.cs" Inherits="Default2" %>

<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Welcome</title>
<link href="UserStyleSheet.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">
function doToggle()
{
var cpe = $find("cpe");
cpe._animation._fps = 30;
cpe._animation._duration = 0.20;

if (cpe.get_Collapsed())
{
cpe._doOpen();
}
else
{
cpe._doClose();
}
}
</script>
</head>

<body>
<form id="form1" runat="server">

<!---------------------------------------------!-->
<asp:ScriptManager ID="ScriptManager" runat="server">
</asp:ScriptManager>
<!---------------------------------------------!-->

<div id="Panels" style="width: 28%;">

<div id="Head1" onclick="doToggle();">
HEADER <asp:Label ID="lbl1" runat="server"></asp:Label>
</div>
<asp:Panel ID="Panel1" runat="server" CssClass="Content">
ASP.NET AJAX is a free framework for quickly creating a new generation of more efficient,
more interactive and highly-personalized Web experiences that work across all the
most popular browsers.
</asp:Panel>

<!---------------------------!-->


<div id="Head2" onclick="doToggle();">
HEADER <asp:Label ID="lbl2" runat="server"></asp:Label>
</div>
<asp:Panel ID="Panel2" runat="server" CssClass="Content">
saadASP.NET AJAX is a free framework for quickly creating a new generation of more efficient,
more interactive and highly-personalized Web experiences that work across all the
most popular browsers.
</asp:Panel>

</div>
<cc1:CollapsiblePanelExtender ID="cpe" runat="server"
TargetControlID="Panel1" CollapsedSize="0" SuppressPostBack="True"
CollapsedText="↓" ExpandedText="↑" TextLabelID="lbl"></cc1:CollapsiblePanelExtender>
<!---------------------------------------------!-->

</form>
</body>


</html>[/HTML]



well heres my code, i want both div tags to call doToggle() onclick event which is working fine. But the target TargetControlID of CollapsiblePanelExtender remains same, which is causing only one div (Head1) to get slided.
Oct 3 '08 #9
You can give same name to all divs.But different ids.Now call this function on onclick of every div as oncllick="doToggle(this);" This is more than enough.Now you do not not need to call independant function for each div
Expand|Select|Wrap|Line Numbers
  1. function doToggle(that){
  2.  
  3. var divName=document.getElementsByName('Name of the div');
  4. var divLength=divName.length;
  5.  
  6. for(i=0;i<divLength;i++)
  7.  
  8.     {
  9.  
  10.     if(divName[i].id==that){alert('Hi Friend');}
  11.     else{alert('Bye Friend');}
  12.     }
  13. }
Hope this could be easy and efficient for you.
Oct 3 '08 #10

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

Similar topics

55
by: Ton den Hartog | last post by:
Stupid basic question but I find it horribly imposible to find the answer elsewhere... :-( I want to have a piece of text in my HTML page and want to be able to change it in a Javascript...
61
by: Toby Austin | last post by:
I'm trying to replace <table>s with <div>s as much as possible. However, I can't figure out how to do the following… <table> <tr> <td valign="top" width="100%">some data that will...
2
by: MOHSEN KASHANI | last post by:
Hi, I am trying to hide some form elements in a form by default and show/hide depending on which radio button is clicked. This is what I have but it is not working: <head> <style> ..noshow {...
2
by: ad | last post by:
When we create a new aspx in VS.Net, it will insert <div> tag inside <form> tag, like below. What is the function of <div> do?...
3
by: Josef K. | last post by:
Asp.net generates the following html when producing RadioButton lists: <td><input id="RadioButtonList_3" type="radio" name="MyRadioButtonList" value="644"...
7
by: simchajoy2000 | last post by:
Hi, I am just a javascript beginner so maybe this is a simple problem but I am trying to do some rollovers on images in a separate <div>. Here is the relevent piece of my code: <html>...
4
by: joe | last post by:
Hi, I defined: <div id="abc"> </div> in my .html file, after an Ajax query my weberserver sends: <script language="JavaScript"> alert('Hello World!"); </script>
1
by: saadkhan | last post by:
I`v made the following code: <div id="Panels" style="width: 28%;"> <asp:Panel ID="one" runat="server" CssClass="Panel"> <div id="Head" onclick="doToggle();"> ...
8
prino
by: prino | last post by:
Hi all, I've written code (in REXX) that takes files in legacy languages (PL/I, COBOL, z/OS assembler, etc) and converts them into HTML in a format similar to what's displayed in the z/OS ISPF...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.