kevinabraham kevinabraham - 1 year ago 87
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 +'\'\)');

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

Answer Source

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


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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download