Solved

jQuery code - clean-up

Posted on 2013-11-11
10
225 Views
Last Modified: 2013-12-10
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

0
Comment
Question by:intoxicated_curveball
  • 4
  • 2
  • 2
10 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 39639373
Your code is fine.

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

Open in new window

0
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 250 total points
ID: 39639434
As far as optimising goes, I'm not sure what you hope to gain here. There's a few ways of doing it but I doubt you'll really gain anything from it:

if ($('#ggl_search').val() != 'Search') $('#ggl_search').css('color', 'black');

Open in new window

0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 39639442
Another one...
	$('#ggl_search[value!="Search"]').css("color","black");

Open in new window

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:intoxicated_curveball
ID: 39639549
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?
0
 
LVL 43

Expert Comment

by:Chris Stanyon
ID: 39639600
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 ;)
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39639610
Check this :
http://jsfiddle.net/Nsn8c/
http://jsfiddle.net/Nsn8c/1/

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

Open in new window

0
 

Author Comment

by:intoxicated_curveball
ID: 39645738
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();
0
 
LVL 82

Expert Comment

by:leakim971
ID: 39645762
check lines 1 and two of my first post...

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question