Link to home
Start Free TrialLog in
Avatar of intoxicated_curveball
intoxicated_curveball

asked on

jQuery code - clean-up

Can you clean up my code? It works but I think it's not optimized. It's just supposed to change the text box to black text on page load in cases when the pre-existing value is not 'Search' (this happens when someone does a search).

	var ggl = $('#ggl_search');
	var ggl_val = $('#ggl_search').val();
	
	if (ggl_val != 'Search') {
		ggl.css("color","black");	
	}

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

Your code is fine.

	var ggl = $('#ggl_search');
	if (ggl_val.val() != 'Search') {
		ggl.css("color","black");	
	}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of intoxicated_curveball
intoxicated_curveball

ASKER

In terms of optimizing I was wondering can't I store the element and the element value in one variable, instead of declaring two vars?
2 different things in one variable, interesting!

My point was, in the grand scheme of things, why would you want to try?

You won't gain anything by doing it (in fact sometimes the code becomes less readable / less manageable and ultimately less efficient!)

Having said that - both leakim and I have given you one line of code that doesn't even use a single variable! Whether it's more 'optimized' is open for debate ;)
Check this :
http://jsfiddle.net/Nsn8c/
http://jsfiddle.net/Nsn8c/1/

#ggl_search:not([value=search]) {
    color: black;
    background: yellow;
}

Open in new window

I think I'm not making myself very clear. Can I not do this?

var ggl = $('#ggl_search');

And Then be able to access the value of the element using ggl.val();

Instead of having to do two variables:

      var ggl = $('#ggl_search');
      var ggl_val = $('#ggl_search').val();
check lines 1 and two of my first post...

var ggl = $('#ggl_search');
      if (ggl_val.val() != 'Search') {