JQuery optimising a simple script

As a beginner a JQuery I am writing scripts.
I know they are a little verbose and simplistic.

I would really appreciate it if someone could optimise my simple script so that I can see how to go about it. I want to learn how to use JQuery in a structured and optimised way.

The script hides and inhides a field according to the text selected (not the VALUE) of a dropdownlist.

<script>
	$().ready(function() {
		$('#title').change(function () {
			if ($("#title option:selected").text() == "Other") {
				$('#titleother').show();
			}
			else {
				$('#titleother').hide();
				$('#titleother').val('');
			}
		});
	});
</script>

Open in new window


Kind Regards,
LVL 2
splantonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
I don't see how you could optimize it further
0
Rainer JeschorCommented:
Hi,
I am not sure (and have only a solid experience in jQuery ) but the following could be a little bit faster as it queries the DOM only once:
	$().ready(function() {
        var titleOtherElement = $('#titleother');
		$('#title').change(function () {
			if ($(this).find("option:selected").text() == "Other") {
				titleOtherElement.show();
			}
			else {
				titleOtherElement.hide().val('');
			}
		});
	});

Open in new window


Just my 2ct
Rainer
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryCommented:
I think any gain in using a single global variable would be lost in the lookup and apart from that using global variables is bad practice when they are not needed (and in general anyway)
Though the chaining is a good tip.
0
splantonAuthor Commented:
Limiting queries to the DOM and substituting repeated references to actual Id's/classes is definitely the way forward. This example was probably a little short, but in longer scripts (from a maintenance point of view) this has to be a good idea. The transposition from DOM calls to 'this' is what was tripping me up. It's good to see the two versions of the same simple script in a way that allows me to see the conversion in action as sometimes code ripped from boards and downloaded examples is tricky to modify and use in your own situations.
Many thanks.
0
GaryCommented:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.