On the .click
event of the <a>
element I adding a class my-selected-item
.
But the class only affect right at the .click
event. I read that i can handle this with the function event.preventDefault
. But this results that the <a>
element doesn't fire the href=""
link.
$(document).ready(function () {
$("a.seaw-menu-item").click(function () {
$(this).addClass('my-selected-item');
});
});
It seems that I missing a little thing. Thanks for your help!
It is for a menu purpose.
<div class="myMenu">
<a href="myPage.php?subpage=mySubPage1" class="seaw-menu-item">MenuItem 1</a>
<a href="myPage.php?subpage=mySubPage2" class="seaw-menu-item">MenuItem 2</a>
<a href="myPage.php?subpage=mySubPage3" class="seaw-menu-item">MenuItem 3</a>
</div>
I can only guess, but I believe you want something like this:
Add a div with id="someContainer" and the content of the page in the href (assuming same origin) will be loaded into it and the class set
Using your HTML:
$(document).ready(function() {
$("a.seaw-menu-item").on("click", function(e) {
e.preventDefault();
$("a.seaw-menu-item").removeClass('my-selected-item'); // remove from all links
console.log("This would load:"+this.href+" into someContainer");
$("#someContainer").load(this.href); // load the page
$(this).addClass('my-selected-item'); // set the class on this link
});
});
a { text-decoration:none }
a.my-selected-item { text-decoration: underline }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="myMenu">
<a href="myPage.php?subpage=mySubPage1" class="seaw-menu-item">MenuItem 1</a>
<a href="myPage.php?subpage=mySubPage2" class="seaw-menu-item">MenuItem 2</a>
<a href="myPage.php?subpage=mySubPage3" class="seaw-menu-item">MenuItem 3</a>
</div>
<div id="someContainer"></div>