Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jQuery code - clean-up

Posted on 2013-11-11
10
Medium Priority
?
231 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 44

Accepted Solution

by:
Chris Stanyon earned 1000 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 1000 total points
ID: 39639442
Another one...
	$('#ggl_search[value!="Search"]').css("color","black");

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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 44

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

650 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