4 Replies - 1238 Views - Last Post: 26 May 2013 - 10:51 PM

#1 P.Conrad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-March 13

reducing code [easy]

Posted 22 May 2013 - 07:07 AM

Hi,

Basically I'm just starting my adventure with Jquery and need a bit of help from you guys.

How to shorten that code to one function cos I'm a profound believer in DRY (Don't repeat yourself).

$(".gotAccount").click(function(event){
		event.preventDefault();

		$(".register").fadeOut( function() {
			$(".login").fadeIn('slow')
			});
	});

	$(".dontHaveAccount").click(function(event){
		event.preventDefault();

		$(".login").fadeOut( function() {
			$(".register").fadeIn('slow')
			});
	});


It simply switches two dives. In div login we have login form and a link .dontHaveAccount, and in div register we have registration form with link .gotAccount.

Thanks guys!

Is This A Good Question/Topic? 0
  • +

Replies To: reducing code [easy]

#2 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 16479
  • View blog
  • Posts: 65,313
  • Joined: 12-June 08

Re: reducing code [easy]

Posted 22 May 2013 - 07:14 AM

Why not have a function that takes in a target (your .register or .login), takes in a fadein target, and a fade in speed.. then just call that function in each event?
Was This Post Helpful? 0
  • +
  • -

#3 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: reducing code [easy]

Posted 22 May 2013 - 07:16 AM

another simplification:
$(".gotAccount, .dontHaveAccount").on("click", function(event) {
    // ...
});

and then something with toggle().

This post has been edited by Dormilich: 22 May 2013 - 07:17 AM

Was This Post Helpful? 0
  • +
  • -

#4 P.Conrad   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 10-March 13

Re: reducing code [easy]

Posted 24 May 2013 - 04:27 AM

I tried something like that Dormilich, but problem is I want to first fadeOut one div and then fadeIn the other. So if I use toggle it will work only one way - the other would be that second div fadeIn and only after the other fadeOut (so for a while I would have two divs visable - which is not necceserly what I'm after) . The solution would be, if I have your code

$(".gotAccount, .dontHaveAccount").on("click", function(event) {



to asses which precise link was clicked and then use if statement. But can I acquire that in jquery ?
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4303
  • View blog
  • Posts: 13,677
  • Joined: 08-June 10

Re: reducing code [easy]

Posted 26 May 2013 - 10:51 PM

View PostP.Conrad, on 24 May 2013 - 01:27 PM, said:

to asses which precise link was clicked and then use if statement.

in an event handler this refers to the current element (e.g. the element clicked).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1