Solved

jQuery code - clean-up

Posted on 2013-11-11
10
226 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

749 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