Load and unload a page into a div from a navigation button - html

Can anyone tell me in the simplest code possible, how to load and unload an html page into a div named "myDiv", with a simple click?
When i press another button on my navigation menu, i will then unload "myDiv" and replace with another page.
Whats the setup for this?
edit
I have 3 pages (page1.html, page2.html, page3.html)
My navigation is as follows:
<ul>
<li></li>
<li></li>
<li></li>
</ul>
I am trying to load those pages into "myDiv", each page replacing the previous loaded page everytime i click a different page button. Thats all.
I hope i made what im trying to do clear as crystal and hopefully not leaving anything important out.

Assuming you can use javascript/jQuery (you don't give a lot of info on your environment)...
Have a look at the jQuery load() method.
http://api.jquery.com/load/
<div id="myDiv"></div>
<button id="myButton">Click Me</button>
<script type="text/javascript">
$( document ).ready( function() {
$( '#myButton' ).click( function() {
$( '#myDiv' ).load( 'test.html' );
});
});
</script>
That should do your load. I'll leave the rest to you :)
EDIT:
OK, something more along the lines of what you're looking for...
Assuming you can modify the markup and add a class attribute to your a elements...
<ul>
<li></li>
<li></li>
<li></li>
</ul>
<script type="text/javascript">
$( document ).ready( function() {
$( 'a.dynamicLoad' ).click( function( e ) {
e.preventDefault(); // prevent the browser from following the link
e.stopPropagation(); // prevent the browser from following the link
$( '#myDiv' ).load( $( this ).attr( 'href' ) );
});
});
</script>
So any 'a' element with the class of dynamicLoad will trigger this when clicked. We don't want the browser to try and follow the link, so we use preventDefault() and stopPropagation(). The only other difference is that we're not statically loading "test.html". We're determining what html page to load by using jQuery's $( this ), which represents the element that triggered the function. Use the attr() method, which returns the value of a specific attribute of the element. In this case, the href attribute.
Questions?

Related

Permanently delete DIV from code content by ID on the frontend

I have on my site, a page with several DIV's with some content (let's say that each one is a TO DO task).
I need to view that page URL and choose which DIV I want to delete permanently (in a way that even if I refresh the page, it won't be there anymore).
Is this possible?
I have this code, but the "deleted" DIV re-apear as soon I refresh the page...
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("#removeDIVid1").click(function () {
$("#id1").remove();
});
$("#removeDIVid2").click(function () {
$("#id2").remove();
});
$("#removeDIVid3").click(function () {
$("#id3").remove();
});
});
</script>
</head>
<body>
<div id="id1"><p>paragraph 1 <button id="removeDIVid1">Remove DIVid1</button></p></div>
<div id="id2"><p>paragraph 2 <button id="removeDIVid2">Remove DIVid2</button></p></div>
<div id="id3"><p>paragraph 3 <button id="removeDIVid3">Remove DIVid3</button></p></div>
</body>
</html>
The remove method just takes the object out of the DOM, and when you refresh the page, since the DOM tree is generated again, with your div elements. I think generating these tasks dynamically using jQuery will solve your problem. Let me know if you need help with the code.

Changing css on elements within specific pages

I am trying to change the CSS on specific elements inside the page (since I need them to be different on specific pages) Trying to do this to no avail.
<script>
$(document).find('#nav-icon span').css({
'background-color': '#333'
});
$(document).find('.screen-nav li a').css({
'color': '#fff'
});
$(document).find('.screen-nav li a:after').css({
'background-color': '#fff'
});
</script>
However you are trying to find the html element from document which runs before DOM is ready. Therefore you need to bind you find function after DOM is ready and jquery script should be with in document.ready function and try to change css property from jQuery like this:
HTML
<nav class="screen-nav">
<ul>
<li>AA</li>
<li>BB</li>
<li>CC</li>
<li>DD</li>
</ul>
</nav>
JQUERY
$(document).ready(function(){
$('.screen-nav li a').css({
"background-color": "yellow",
"font-size": "200%",
"color":"#000"
});
});
Working demo here
Its better to execute these CSS changes after the document has completed loading.
$(document).ready(function(){
$(document).find('#nav-icon span').css({
'background-color': '#333'
});
});
You can also reduce the code above to simply query the objects from the class. Since your parent object is the document, you can directly use JQuery selectors on the CSS classes
$(document).ready(function(){
$('#nav-icon span').css({
'background-color': '#333'
});
});

Open every external link in a new window automatically (html, css)

I am searching for an option to open every external links from my website (wordpress-blog) to any other sites automatically in a new window. Is that with css or html possible without doing it 1000 times manually via hand as "target _blank"?
Thank you so much!
PS: sry for my bad english, I am no native speaker :(
Put this code in your theme functions.php file.
function cdx_handel_external_links() {
?>
<script type="text/javascript">
( function( $ ) {
$("a[href^=http]").click(function(){
if(this.href.indexOf(location.hostname) == -1) {
$(this).attr({
target: "_blank"
});
}
})
//Add Nofollow
$("a").each(function(){
if(this.href.indexOf('nytimes.com') >0 ){
$(this).attr({
rel: "nofollow"
});
}
});
} )( jQuery );
</script>
<?php
}
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);
Yes, You can use Open external links in a new window plugin.
It will be helpful to open all or specific external links in a new window.
If you put the following in the head tag of your HTML, any href tag without a target should open in a new window:
<head>
<base target="_blank">
</head>

Semantic UI Accordion not working properly

I have an accordion element in my page. The problem is that the accordion appears on the page but it is not clickable. By 'not clickable', I mean that when I click on the header it does not expand to reveal the contents. Nothing happens at all. I hope someone can help.
Thanks in advance.
Your jQuery.js module must be loaded before the semantic-ui accordion.js
module.
Simply put
<script src="js/accordion.js"></script>
after
<script src="js/vendor/jquery-1.11.2.min.js"><\/script>
( or whatever your jQuery version is ... )
and initialize the accordion in the html document inside a script tag as :
<script language='javascript'>
$(document).ready(function(){
$('.ui.accordion').accordion();
});
</script>
It happens on nested accordions while you script is under $( document ).ready(function()
So try to call accordion function in an ajax callback like this;
$('input[name=sampleInput]').on('input', function() {
var val = $("input[name=sampleInput]").val();
if (val.length >= 3)
{
$.ajax( {
url: 'sample_handler.php',
type: 'GET',
data: {
data: data
},
dataType: 'html',
success: function ( response ) {
$('.ui.accordion').accordion({});
}
})
}
})
For instance, I've put accordion function in a callback. So I could use it again and again, even I add nested accordions.
In my case I had syntax errors inside javascript/jQuery. After fixing that and importing jQuery module before semantic-ui it works. You can open development tools in the browser and check the console for errors in javascript (F12 in Chrome).
<script type="text/javascript">
$(document).ready(function() {
window.onload = function(){
$('.ui.accordion').accordion();
};
});
</script>

Adding onclick event on all links on my website

I need to apply this line:
target="_blank" onclick="exoMobilePop();"
To all links on my Drupal 7 website.
Can anybody help me with this?
If you really need this functionality on links and not on the whole document you can go with this solution.
...
<script>
(function() {
var linksOnPage = document.querySelectorAll("a");
var link = "";
for (var i = 0; i < linksOnPage.length; i++) {
link = linksOnPage[i];
link.setAttribute("target", "_blank");
link.addEventListener("click", function(e){
exoMobilePop();
});
}
})();
</script>
</body>
</html>
But be careful. This will address literally every link on your page. Including administrative links, menu items, etc. If it's not desired, you can replace "a" with a more specific selector.
Try the <base> tag. The target attribute specifies the default target for all hyperlinks and forms in the page. Place the tag in the <head> section.
Note: This attribute can be overridden by using the target attribute for each hyperlink/form (if needed).
<head>
...
<base target="_blank">
...
</head>
Use JavaScript (or jQuery) to handle and reroute the click events. JSFiddle
JavaScript
document.addEventListener("click", function (e) {
// e.preventDefault(); // Prevent a link from following the URL
exoMobilePop();
});
jQuery
$("body").on("click", "a", function (e) {
// e.preventDefault(); // Prevent a link from following the URL
exoMobilePop();
});