Javascript variables

David

Probably it's easy but I can't figure it out. I have this code:

var myNewHref = "#"+jQuery(this).attr('id');
jQuery(this).attr('href', myNewHref);
jQuery(this).attr("id", "");

In the first line I create new variable based on id of element. In second line I add a href property with myNewHref variable. In the third line I set id to be empty. The problem is that if I set my id to be empty it also changes myNewHref variable and makes it empty, but I want it to be still set with myNewHref. Besides action of clearing id takes place after I assigned it to my href. Anyone can help?

Rafael Herscovici

seems to work just fine:

http://jsfiddle.net/4y2HM/4/

<a href="#" id="test">hello</a>

<script>
    $('#test').click(function(){
        var myNewHref = "#"+$(this).attr('id');
        $(this).attr('href', myNewHref);
        $(this).attr("id", "");
        alert("href: " + $(this).attr('href'));
        alert("id: " + $(this).attr('id'));
    });
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related