To change the href
of an element you can do like this:
elemento.href = 'http://stackoverflow.com';
You can also use the .setAttribute
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 what you want is to avoid having the symbol in the URL #
, can use href="javascript:void(0)"
. If you don’t have it like that at first, you can use:
$("a").attr("href", "javascript:void(0)"); // talvez queira ser mais específco e usar $("#meuSlideShow a")...
Use #
is an old and very useful trick. That #
go to the URL can be avoided with the .preventDefault()
also. But it seems to me that is the wrong tool if what you want is just not to show the #
.
But for this feature does not need an anchor, can work as well or better with another element like <li>
, <fiv>
or even <button>
...
Now seeing your HTML and CSS, here is 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%';
});
True, I had completely forgotten the void(0). Perfect!
– rafaels88
Guy saved the fatherland Thanks man! Solved in 100%!
– Lauro Moraes