0

I have some code that saves a selector in a variable, which when testing works fine the first time I reference it (line 2 of my code), but when I try to add an h1 to it, seen on line 3 of my code, I get the following error: "Uncaught Error: Syntax error, unrecognized expression: [object Object] h1"

How can I add an h1 to my variable in the selector without getting an error?

var $tabContentHeader = $('.tab-content-header');
var $headerLineHeight = $($tabContentHeader).height() + 10 + 'px';
$($tabContentHeader + ' h1').css({'line-height': $headerLineHeight});

HTML:

<div class="row tab-content-header">
    <img src="images/content-area/page-header.png" class="img-responsive" alt="a1">
    <h1>Welcome</h1>
</div>
2
  • please show your relevant html Commented Oct 7, 2015 at 14:49
  • 2
    There's no need for the second jQuery wrap: $($tabContentHeader). Just stay with $tabContentHeader Commented Oct 7, 2015 at 14:52

1 Answer 1

1
var $tabContentHeader = $('.tab-content-header');
var $headerLineHeight = $($tabContentHeader).height() + 10 + 'px';
$tabContentHeader.find('h1').css({'line-height': $headerLineHeight});
Sign up to request clarification or add additional context in comments.

2 Comments

This looks to be exactly what I was looking for. Thank you.
I have several instances of .tab-content-header h1, but this code appears to only change the first instance of it. How can I alter the code to get it to change all instances of .tab-content-header h1 at once?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.