Reputation:
I have a series of buttons that I want to be clicked depending upon the hash at the end of the page's URL, which can change depending on interactivity with the page. I can accomplish this fine by selecting the buttons by their class (.fullscreen-button') and by which number they are in the array of buttons, but I can't select the same buttons by using their unique IDs. I'd rather select them by their IDs because their order in the array can change.
Here is my function that works via the class method...
$(function() {
if ( document.location.href.indexOf('#Programming') > -1 ) {
$('.fullscreen-button')[0].click();
} else if ( document.location.href.indexOf('#3D') > -1 ) {
$('.fullscreen-button')[1].click();
} else if ( document.location.href.indexOf('#Acting') > -1 ) {
$('.fullscreen-button')[2].click();
} else if ( document.location.href.indexOf('#Audio') > -1 ) {
$('.fullscreen-button')[3].click();
}
});
...and this is what I attempted for the ID method but which doesn't work...
$(function() {
if ( document.location.href.indexOf('#Programming') > -1 ) {
$('#Programming for Web').click();
} else if ( document.location.href.indexOf('#3D') > -1 ) {
$('#3D Design').click();
} else if ( document.location.href.indexOf('#Acting') > -1 ) {
$('#Acting').click();
} else if ( document.location.href.indexOf('#Audio') > -1 ) {
$('#Audio Editing').click();
}
});
Why doesn't selecting the objects by their IDs work in this case?
EDIT: Alright, it's those dang spaces. Thanks to all for the help!
Upvotes: 0
Views: 445
Reputation: 11983
You aren't supposed to use spaces in your ID strings. http://www.w3.org/TR/html4/types.html
However if you do, you need to escape the space in jquery to select it. Otherwise $("#Programming for web")
would be looking for the <web>
in:
<div id="Programming">
<for>
<web/>
</for>
</div>
To select [this invalid] ID string you would use: $("#Programming\\ for\\ web")
Upvotes: 2
Reputation: 761
you can't have spaces in div id names that's why just remove the spaces and it will work :)
Upvotes: 0