0

Quick overview, when a user clicks a link with an anchor tag it opens the closest hidden div to that anchor on the destination page.

My problem seems pretty basic I just can't figure it out.

Why does this work(specifying the variable to set the height to, in this case height7):

var height7 =  100;

if(window.location.hash) {
      var hash = window.location.hash.substring(1); 
      $('a[name='+hash+']').closest('[id^="option"]').show();
      $('a[name='+hash+']').closest('[id^="option"]').height(height7);
} else {
      // No hash found
}

And this not work(in this case trying to build the name of the div i want to open, place it in a variable and passing it to the height() function exactly as above, for some reason it doesn't accept the variable):

if(window.location.hash) {
     var hash = window.location.hash.substring(1); 
     var option_name = $('a[name='+hash+']').closest('[id^="option"]').attr("id");
     var hash_div_height_id = "height" + option_name.substring(6);
     alert(hash_div_height_id);
     $('a[name='+hash+']').closest('[id^="option"]').show();
     $('a[name='+hash+']').closest('[id^="option"]').height(hash_div_height_id);
} else {
      // No hash found
}
1

2 Answers 2

1

You're assigning different values in each case:

$('a[name='+hash+']').closest('[id^="option"]').height(height7); //height = 100

$('a[name='+hash+']').closest('[id^="option"]').height(hash_div_height_id); //height = "height" + option_name.substring(6)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, should have realized that.
1

You seem to be assigning a string value

var hash_div_height_id = "height" + option_name.substring(6);

     .height(hash_div_height_id);

Where as it is supposed to be a number.

So hash_div_height_id will be something like height + something

When setting a height property it expects

An integer representing the number of pixels, or an integer with an optional unit of measure appended (as a string).

2 Comments

Thanks for identifying my issue. Now I have the slightly bigger problem of figuring out how to take the string "height7" and somehow using that to find out the value of the variable height7.
It is difficult to say without taking a look at the HTML

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.