Toggle or Add and Remove ID of an Element on Click with jQuery

moderntimes

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

Abhitalks

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Remove element on click with Jquery

From Dev

How to add/remove class of child element on click of var ID?

From Dev

jQuery - Add active class and remove active from other element on click

From Dev

Toggle click, add an remove classes with Js

From Dev

jQuery: remove element by id

From Dev

jquery toggle img click and store or remove img id from local storage when clicked

From Dev

jQuery click and toggle not working with class or ID

From Dev

Three way toggle to add and remove classes in jQuery

From Dev

jQuery Toggle add/remove on same button to a list

From Dev

JQuery Active toggle/add/remove class issue

From Dev

jQuery add/remove class or toggle class

From Dev

jQuery Add / Remove Class on Click

From Dev

Jquery Remove/Add Image on Click

From Dev

Selenium - click on a toggle element

From Dev

Jquery add class to element on click, remove that same class from previous item.

From Dev

Remove Appended Element on Second Click jQuery

From Dev

Remove div element on click jQuery not working

From Dev

JQuery click event remove ul li element

From Dev

on click toggle for child element of the certain class (handlers) - jquery

From Dev

jquery .on('click') toggle css of this li element + call function same time

From Dev

Using jQuery click event to toggle element on page using "this"

From Dev

jQuery: Toggle class on one element, remove same from all other

From Dev

jQuery - Click Add/Remove Class - Multiple Buttons

From Dev

Add and remove a class on click using jQuery?

From Dev

JQuery click event to add or remove table rows

From Dev

Add and remove a class on click using jQuery?

From Dev

jQuery - Click Add/Remove Class - Multiple Buttons

From Dev

Add/remove content on click button via jquery

From Dev

Jquery add and remove several divs with one click

Related Related

  1. 1

    Remove element on click with Jquery

  2. 2

    How to add/remove class of child element on click of var ID?

  3. 3

    jQuery - Add active class and remove active from other element on click

  4. 4

    Toggle click, add an remove classes with Js

  5. 5

    jQuery: remove element by id

  6. 6

    jquery toggle img click and store or remove img id from local storage when clicked

  7. 7

    jQuery click and toggle not working with class or ID

  8. 8

    Three way toggle to add and remove classes in jQuery

  9. 9

    jQuery Toggle add/remove on same button to a list

  10. 10

    JQuery Active toggle/add/remove class issue

  11. 11

    jQuery add/remove class or toggle class

  12. 12

    jQuery Add / Remove Class on Click

  13. 13

    Jquery Remove/Add Image on Click

  14. 14

    Selenium - click on a toggle element

  15. 15

    Jquery add class to element on click, remove that same class from previous item.

  16. 16

    Remove Appended Element on Second Click jQuery

  17. 17

    Remove div element on click jQuery not working

  18. 18

    JQuery click event remove ul li element

  19. 19

    on click toggle for child element of the certain class (handlers) - jquery

  20. 20

    jquery .on('click') toggle css of this li element + call function same time

  21. 21

    Using jQuery click event to toggle element on page using "this"

  22. 22

    jQuery: Toggle class on one element, remove same from all other

  23. 23

    jQuery - Click Add/Remove Class - Multiple Buttons

  24. 24

    Add and remove a class on click using jQuery?

  25. 25

    JQuery click event to add or remove table rows

  26. 26

    Add and remove a class on click using jQuery?

  27. 27

    jQuery - Click Add/Remove Class - Multiple Buttons

  28. 28

    Add/remove content on click button via jquery

  29. 29

    Jquery add and remove several divs with one click

HotTag

Archive