Reputation: 2315
I have a tab set with unique id for each. And a function with unique className from the upload button in each tab. So I want to grab the hash value to build a new className dynamically. To use with the function I built.
Example:
URL : www.myweb.com/upload.php#tab1
jS
var hash=location.hash;//grab hash = tab1
$('.upload_'.hash).uploadPic({//expect className= '.upload_tab1'
.
.
.
});
html
<button id="upload_tab1">Browse</button>
I've tried but it is not working for some reasons. No error, nothing. Please help.
Upvotes: 2
Views: 506
Reputation: 97381
As others have mentioned, you need to use +
instead of .
to concatenate strings in JavaScript.
However, that will still not solve your issue, because the value returned by window.location.hash
, includes the hash (#
) sign that precedes the fragment:
const hash = window.location.hash; // value is "#tab1"
So you need to strip off the hash sign:
const hash = window.location.hash.substring(1); // value is "tab1"
To prevent errors in cases where the fragment is not specified, you might want to check for the presence of the hash sign first:
let hash = window.location.hash;
hash = hash.startsWith('#') ? hash.substring(1) : hash;
Upvotes: 1