11 Replies - 5850 Views - Last Post: 28 April 2011 - 07:34 AM

#1 memoisme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-April 11

passing parameter on click event

Posted 28 April 2011 - 04:33 AM

i dont know how to do this?
<script language="javascript" type="text/javascript">
 function ShowHideDiv()
 {
   var myDiv = document.getElementById("LoginDiv");

   if(myDiv.style.display=="none" && '<%=_Type %>' =="Sign in")
   {
     myDiv.style.display="";
   }
   else
   {
     myDiv.style.display="none";
   }
 }
</script>
<form>
 <input id="crossLogInDiv" type="button" value ="X" onclick ="ShowHideDiv(crossLogInDiv); return false" />
</form>



just tell me how to pass parameter from this function ShowHideDiv() and how to use this?

Is This A Good Question/Topic? 1
  • +

Replies To: passing parameter on click event

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10183
  • View blog
  • Posts: 37,596
  • Joined: 27-December 08

Re: passing parameter on click event

Posted 28 April 2011 - 04:37 AM

Moved to Javascript. Please do not post questions in the tutorials section.

You don't define your ShowHideDiv() function to accept a parameter. You must do such.

An example:
function foo(paramOne, paramTwo){}


This post has been edited by Dormilich: 28 April 2011 - 04:54 AM
Reason for edit:: fixed syntax

Was This Post Helpful? 0
  • +
  • -

#3 memoisme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-April 11

Re: passing parameter on click event

Posted 28 April 2011 - 04:41 AM

okay but where i am posting my qtn its length is just 5 to 6 words only so i need some extra space to explin more..
and how will i use these parameter?
Was This Post Helpful? 0
  • +
  • -

#4 rajesh.kakawat  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 43
  • Joined: 03-January 11

Re: passing parameter on click event

Posted 28 April 2011 - 04:49 AM

explain your problem clearly.
what you want to do ???
Was This Post Helpful? 0
  • +
  • -

#5 memoisme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-April 11

Re: passing parameter on click event

Posted 28 April 2011 - 05:00 AM

function ShowHideDiv(var x)
 {
     if(x =="crossLogInDiv")
     {
     var y= document .getElementById ("crossLogInDiv");
     y.style .display="none";
     }
     if(x =="crossSignUpDiv")
     {
     var z= document .getElementById ("crossSignUpDiv");
     z.style .display="none";
     }
}

                <input id="crossLogInDiv" type="button" value ="X" onclick ="ShowHideDiv(crossLogInDiv); return false" />



on clicking this button this function is not calling?
Was This Post Helpful? 0
  • +
  • -

#6 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: passing parameter on click event

Posted 28 April 2011 - 05:32 AM

Hi

try this,
/*Function*/
function ShowHideDiv(x)


/*Button*/
 <input id="crossLogInDiv" type="button" value ="X" onclick ="ShowHideDiv(crossLogInDiv); return false;" />


That should work



No need for var keyword in

function when recieving parameters ,that will cause the script to fail i think..

This
(var x)

should be

(x)

This post has been edited by marinus: 28 April 2011 - 05:38 AM

Was This Post Helpful? 2
  • +
  • -

#7 memoisme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 27-April 11

Re: passing parameter on click event

Posted 28 April 2011 - 05:45 AM

thanks marinus it really work in my case..
Was This Post Helpful? 0
  • +
  • -

#8 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: passing parameter on click event

Posted 28 April 2011 - 06:27 AM

View Postmarinus, on 28 April 2011 - 01:32 PM, said:

That should work

but only in IE. all other browsers will get an error as crossLogInDiv is an undefined global variable (only IE silently puts names and ids into global vars).
Was This Post Helpful? 2
  • +
  • -

#9 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: passing parameter on click event

Posted 28 April 2011 - 06:50 AM

You don't have to put in var the parameters if that is what you mean..

Then it still work's in all browsers , i have been coding JS like that for ages.

You only have to pass in the value , and not a data type in functions , because dynamic languages can get values assigned late , as suppose to static languages
Was This Post Helpful? 0
  • +
  • -

#10 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3636
  • View blog
  • Posts: 5,758
  • Joined: 08-June 10

Re: passing parameter on click event

Posted 28 April 2011 - 07:17 AM

View PostDormilich, on 28 April 2011 - 01:27 PM, said:

but only in IE. all other browsers will get an error as crossLogInDiv is an undefined global variable (only IE silently puts names and ids into global vars).

That's true, but this case is the exception. Within the "on" event attributes of an element, the ID and/or NAME of the elements are synonymous to "this".

I'd still use "this" over the name/id. Removes any confusion about what is being passed.
Was This Post Helpful? 1
  • +
  • -

#11 marinus  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 135
  • View blog
  • Posts: 575
  • Joined: 14-April 10

Re: passing parameter on click event

Posted 28 April 2011 - 07:26 AM

View PostAtli, on 28 April 2011 - 02:17 PM, said:

View PostDormilich, on 28 April 2011 - 01:27 PM, said:

but only in IE. all other browsers will get an error as crossLogInDiv is an undefined global variable (only IE silently puts names and ids into global vars).

That's true, but this case is the exception. Within the "on" event attributes of an element, the ID and/or NAME of the elements are synonymous to "this".

I'd still use "this" over the name/id. Removes any confusion about what is being passed.


Oh i didn't see that , The OP passed in an id , which is unknown, you should always use

FuntionName(this) as ATLI explained

in the OP case it will be
ShowHideDiv(this.id)

This post has been edited by marinus: 28 April 2011 - 07:34 AM

Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3392
  • View blog
  • Posts: 9,589
  • Joined: 08-June 10

Re: passing parameter on click event

Posted 28 April 2011 - 07:34 AM

View PostAtli, on 28 April 2011 - 03:17 PM, said:

That's true, but this case is the exception. Within the "on" event attributes of an element, the ID and/or NAME of the elements are synonymous to "this".

good to know.

but I still prefer event listeners, much less trouble (in a way).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1