kevinabraham kevinabraham - 3 months ago 12
jQuery Question

How to use hash value to open a bootstrap modal using jquery?

Im using jquery to get the hash value from the URL and then open a modal based on that value. So far my code is:

$(function () {
var activeModal = $('\$\(\'' + location.hash +'\'\)');
console.log(activeModal);
activeModal.modal('show');
});


But when I run this I get an error that reads:
jquery.min.js:2 Uncaught Error: Syntax error, unrecognized expression: $('#modal-name')
.

Answer

If your location.hash is (for example) #somehash, then you would want to get the element using:

$('#somehash')

So why you need the '\$\(\'' inside?
You can just use:

var activeModal = $(location.hash);

What your code does is basically

 var activeModal = $("$('#modal-name')")

Which is not what you wanted.