I want to display the href
link in the <div id="display"></div>
tag so when I press anything in the menu or in my list it'll just open in the div with display
as its id
.
I have this menu like this done
<div class="menu">
<a href="" onkeydown="myFunc()">HOME</a>
</div>
<div id="display"></div>
and my JavaScript is like this
$('#display').html($('.menu a').html());
I don't know much about javascript, but I think the javascript code is actually wrong, I would appreciate is someone would help me.
I want to display the href
link in the <div id="display"></div>
tag so when I press anything in the menu or in my list it'll just open in the div with display
as its id
.
I have this menu like this done
<div class="menu">
<a href="http://stackoverflow." onkeydown="myFunc()">HOME</a>
</div>
<div id="display"></div>
and my JavaScript is like this
$('#display').html($('.menu a').html());
I don't know much about javascript, but I think the javascript code is actually wrong, I would appreciate is someone would help me.
Share Improve this question edited Jul 25, 2019 at 20:15 Seth McClaine 10.1k7 gold badges42 silver badges67 bronze badges asked Oct 8, 2013 at 14:37 Ace MunimAce Munim 3254 silver badges18 bronze badges5 Answers
Reset to default 3I want to display the href
You need to fetch href
property for that you can use .prop()
$('#display').html($('.menu a').prop('href'));
Demo
In case you mean retrieve the page and place it in the div:
// bind click event to all anchors within .menu
$('.menu a').click(function(e){
// fetch the page using AJAX and place the results in #display
$('#display').load(this.href);
// prevent navigating away from the page (default action of anchor)
e.preventDefault();
});
(Or maybe it's just me, but the question seems very hard to understand. :shrug:)
$('.menu a').on('click',function(e){
e.preventDefault(); //this will keep your link from loading
var href = $(e.currentTarget()).attr('href');
$('#display').html(href);
});
We can use an iframe to display the link in the <a>
tag.
Here's a fiddle
Here is my version...
HTML
<div class="menu">
<a id="xxx" href="http://stackoverflow." onkeydown="myFunc()">HOME</a>
</div>
<div id="display"></div>
JS
$(document).ready(function() {
var data = $("a#xxx").attr("href");
$('#display').html(data);
});