I have a nav with a few nav links. I have to assign an id for the nav links to work with a specific script. Unfortunately I cannot use classes with the script.
Setting the new id for the element works fine, the script picks up the event and does its job. Giving back the original id to the nav link once any other link on the page or another nav link is clicked also works but I am not sure if this is the right approach.
My code seems to give the new id to the element merely for the time of the click (what is desirable) however I wonder how I can toggle or add and remove the new id, only once I click on any other link (including nav links) on the page. Not sure if having the new id on the element merely for the time of the click is a good thing.
https://stackoverflow.com/questions/11489037/add-and-remove-attribute-with-jquery/11489050#11489050
and
https://stackoverflow.com/questions/7077673/add-and-remove-class-on-click/7077753#7077753
and
https://stackoverflow.com/questions/15418219/add-an-id-to-clicked-links-but-remove-it-from-others/15418623#15418623 come very close to what I want to achieve however those questions are either with classes or separate buttons.
HTML
<nav>
<ul>
<li><a class="navlink" id="Work" href="#Work">Work</a></li>
<li><a class="navlink" id="Portfolio" href="#Portfolio">Portfolio</a></li>
<li><a class="navlink" id="Print" href="#Print">Print</a></li>
<li><a class="navlink" id="Services" href="#Services">Services</a></li>
<li><a class="navlink" id="Contact" href="#Contact">Contact</a></li>
</ul>
</nav>
jQuery
// bind event handler to element/s
$('.navlink').stop(true).click(function (e) {
// prevent default action
e.preventDefault();
// get the id of the clicked element
var currentId = $(this).attr('id');
// show alert with the id of the clicked element
alert( "The current ID of the clicked element is "+ currentId +" ");
// change id of the element to newId
{this.id = "newId";}
// run script on clicked element
$(this).scrolld();
// give element back its original id it had before click
// not sure if this is ok
// it does work but I think there is better or more elegant ways
{this.id = currentId;}
// instead of {this.id = currentId;} I am trying to do something like this
// but it does not seem to work
// I would like the new id to stay with the element until any other link
// on the page is clicked and not only for the duration of the click
$('.navlink').click(function(){
$('.navlink').removeAttr("id")
$(this).attr("id", currentId);
});
});
What would be the correct way to code this so that really the element is given the new id once clicked and given back its original id once any other element on the page is clicked?
I am not sure if having the new id on the element for the time of the click is done the right way or at least acceptable.
It works, however I am not sure, seems too easy somehow, if you can help me come up with a better or more solid way, please do share your thoughts and thanks in advance.
Best Regards
Are you looking for something like this:
Demo: http://jsfiddle.net/abhitalks/5JuBe/2/
Check console
for logged actions.
The idea is to cache the active link for which you need to change the id. Restore the original cached id once another link is clicked.
var $prevLink = null, prevId = null;
$('.navlink').stop(true).click(function (e) {
if (prevId) {
// restore current link and id if not previously cached
$prevLink.attr('id', prevId);
console.log($prevLink.text() + ' restored to ' + $prevLink.attr('id'));
}
// cache current link and id
$prevLink = $(this);
prevId = this.id;
console.log($(this).text() + ' cached with ' + prevId);
// change id of the element to newId
this.id = "newId";
console.log($(this).text() + ' changed to ' + this.id);
// carry out your operation on this link here
});
Footnote: Changing ids is not a very good idea though. Also, make sure that your "newId" does not conflict with any other element.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments