To change the href
of an element you can do this:
elemento.href = 'http://stackoverflow.com';
You can also use .setAttribute
that changes in HTML directly:
elemento.setAttribute('href', 'http://stackoverflow.com');
are different ways, with some differences .
With jQuery you can do with .attr()
or .prop()
following the same logic:
$("a").attr("href", "http://www.outra.coisa/");
If you want to avoid having the #
symbol in the URL, you can use href="javascript:void(0)"
. If you do not have it this way, you can use:
$("a").attr("href", "javascript:void(0)"); // talvez queira ser mais específco e usar $("#meuSlideShow a")...
Using #
is an old and very useful trick. This #
stop at URL can be avoided with .preventDefault()
too. But it seems to me that it is the wrong tool if what you want is just not showing #
.
But for this functionality does not need an anchor, it can work as well or better with another element like <li>
, <fiv>
or even <button>
...
Now seeing your HTML and CSS, here's a new solution with jQuery:
var elementosMenu = $('.slide_menu .panel');
$('#page a[id^="link-"]').on('click', function(e){
e.preventDefault();
elementosMenu.css('margin-left', '-102%');
var targetID = this.getAttribute('href').substr(1);
document.getElementById(targetID).style.marginLeft = '0%';
});
jsFiddle: link