Solved

jQuery code - clean-up

Posted on 2013-11-11
10
222 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 70
How to use embargo Date/time in php 29 35
Scroll 5 news at a time. 4 25
"Pause" execution of javascript function for x miliseconds 2 22
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…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now