jQuery: Error if class not found

This causes a JavaScript error on pages that do NOT contain:


<a href="hello/" class="hello selected"><em>Hello</em></a>


If no link has a class of selected I get a JavaScript error.
$('#menuSide a.selected').find("em").css({'background-position': $('#menuSide a.selected').find("em").css("background-position").match(/([^\s]+)/)[0] + ' -13px'});

Open in new window

LVL 16
hankknightAsked:
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.

Gurvinder Pal SinghCommented:
it is because background-position property is null.

did you tried after setting background-position property?

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
Gurvinder Pal SinghCommented:
try this code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<script src="jquery-1.4.2.min.js"></script>
	<script>
		$(document).ready(function(){
			var backgroundPosition = $('#menuSide a.selected').find("em").css("background-position");

			if ( !backgroundPosition || backgroundPosition.length == 0)
			{
				alert("background-position is not set");
			}

			var abc = backgroundPosition.match(/([^\s]+)/)[0] + ' -13px';
			$('#menuSide a.selected').each(function(){
				$(this).find("em").css('background-position', abc);
			});
		});
	</script>
</HEAD>

<BODY>
	<div id="menuSide">
		<a href="hello/" class="hello selected">
			<em>Hello</em>
		</a>
	</div>
</BODY>
</HTML>

Open in new window

hankknightAuthor Commented:
How can I get it to work even if the CSS has not set a background position for an item on that page?  I think this should be fixed with JavaScript not CSS.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

Gurvinder Pal SinghCommented:
you can assume background position to be center (50%), if not given.
May be depending upon browser, it may vary
hankknightAuthor Commented:
OK, I see what you mean,  thanks.  This works:
if ($('#menuSide a.selected').find("em").css("background-position")) $('#menuSide a.selected').find("em").css({'background-position': $('#menuSide a.selected').find("em").css("background-position").match(/([^\s]+)/)[0] + ' -13px'});

Open in new window

Gurvinder Pal SinghCommented:
yes, or you can refactor that part out.

well, you are a master yourself :)
hankknightAuthor Commented:
Thanks for the help.  I am still trying to master jQuery!

This also revealed a problem with IE, but I will ask a related question about that.
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.