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");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ;)
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/
http://jsfiddle.net/Nsn8c/
http://jsfiddle.net/Nsn8c/1/
#ggl_search:not([value=search]) {
color: black;
background: yellow;
}
ASKER
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();
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') {
var ggl = $('#ggl_search');
if (ggl_val.val() != 'Search') {
Open in new window