javascript - Click on an object, get another object? -
on click objects id.
the id 'about-me'.
now in on click function want target object that's called 'content-about-me'.
how can this? ive tried no luck:
$('.sub-nav').on('click', function(){ $('#content-'$(this).attr('id')); });
you have concatenate id prefix content-
:
$('#content-' + this.id);
i replaced $(this).attr('id')
equivalent shorter this.id
.
keep in mind instead of doing this, might idea have full id of "other" element stored somewhere on click target. example:
<div class="sub-nav" id="foo" data-other-element="content-foo">...</div>
and do
$('.sub-nav').on('click', function(){ var $other = $("#" + $(this).attr('data-other-element-')); });
this code dry, if decide change id tagging scheme js code not need change well.
Comments
Post a Comment