1

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
}
jancha
  • 4,916
  • 1
  • 24
  • 39
javArc
  • 317
  • 2
  • 6
  • 14
  • I think that you should use `.slice` instead of `.substring` http://stackoverflow.com/questions/2243824/what-is-the-difference-between-string-slice-and-string-substring-in-javascript – Andreas Louv Jun 17 '13 at 22:36

2 Answers2

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).

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • 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. – javArc Jun 18 '13 at 16:27
  • It is difficult to say without taking a look at the `HTML` – Sushanth -- Jun 18 '13 at 17:08
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)
ikumen
  • 11,275
  • 4
  • 41
  • 41