How do I trigger the click event on the span that doesn't have an id? The span in question that refuses to trigger is one that's ID equals "". It doesn't do anything when I click the back or forwards button on the browser, whereas all other ones trigger the click event that loads the appropriate content.
<span id=""></span>
<span id="last_part_match"></span>
This is what I'm trying to do, but it doesn't work and breaks the code. My original code just had the trigger on the last_part ID, but that didn't work either cause the id = "".
window.addEventListener("popstate", function() {
var url = $(location).attr('href');
var parts = url.split("/");
var last_part = parts[parts.length-1];
if((".dashboard_post_nav span").attr('id') == last_part) {
$("#" + last_part).trigger("click");
} else {
$(".dashboard_post_nav span:first-of-type").trigger("click");
}
});
This is the code doing the history push, in case was needed. dash_switch is just the selector for appropriately loading the right content in the click event.
if(!e.isTrigger) {
if(history.pushState) {
if(dash_switch == '') {
history.pushState(null, null, '/');
} else {
history.pushState(null, null, '/' + dash_url + '/' + dash_switch + '');
}
} else {
if(dash_switch == '') {
location.hash = '/';
} else {
location.hash = '/' + dash_url + '/' + dash_switch + '';
}
}
}
Instead of using attr()
use the id as a selector and check length of resultant
if( $('#' + last_part).length ) {