Using Element.insertAdjacentHTML to add 'Blocks' of HTML - html

I'm trying to insert the following 'block' of HTML using Element.insertAdjacentHTML()
<div class="content-wrapper">
<ul>
<li class="go-back-environment">
<a href="/environment">
<p>Back to Environment</p>
</a>
</li>
<li class="back-home">
<a href="/home">
<p>Back to home</p>
</a>
</li>
</ul>
</div>
However, I'm not sure it is possible to add divs with classes and lists. I've been trying to find (unsuccessfully) articles with examples where more than one 'p' or 'span' was added.
I started with the following but I don't know how to continue building:
var footer = document.getElementById ('environment');
footer.insertAdjacentHTML('afterend','')
I tried the following:
<script>
var footer = document.getElementById ('environment');
footer.insertAdjacentHTML('afterend',
'<div class="content-wrapper">
<ul>
<li class="go-back-environment">
<a href="/environment">
<p>Back to Environment</p>
</a>
</li>
<li class="back-home">
<a href="/home">
<p>Back to home</p>
</a>
</li>
</ul>
</div> ');
</script>
Is it even possible ?
Thanks !

Yes, it is. Try using template literals for longer html strings:
var footer = document.getElementById('footer');
var html = `
<div class="content-wrapper">
<ul>
<li class="go-back-environment">
<a href="/environment">
<p>Back to Environment</p>
</a>
</li>
<li class="back-home">
<a href="/home">
<p>Back to home</p>
</a>
</li>
</ul>
</div>
`;
footer.insertAdjacentHTML('afterend', html);
<main>Main</main>
<footer id="footer">Footer</footer>

Looks like you have done some mistake in string
Check for this
var footer = document.getElementById('environment');
var appendHtml = ['<div class="content-wrapper">',
'<ul>',
'<li class = "go-back-environment">',
'<a href = "/environment">',
'<p> Back to Environment </p>',
'</a>',
'</li>',
'<li class = "back-home">',
'<a href = "/home">',
'<p> Back to home </p>',
'</a>',
'</li>',
'</ul>',
'</div>'].join("");
footer.insertAdjacentHTML('afterend', appendHtml);
<div id=environment>Footer Div</div>

Related

How can I get images into my li from a wordpress gallery with a loop

I'm making a WordPress website and I created everything outside of it. Now i'm getting it all together but I'm pretty new to this. I would like to create a Wordpress loop to get images from a gallery, replacing the img tag.
------- Edited -------
So there was a small change in the design but same problem. I need to get the images from a gallery in Wordpress to fill two slideshows with the same images.
<div id="slider1">
<ul>
<li class="mySlides1">
<img src="images_1.jpg">
</li>
<li class="mySlides1">
<img src="images_3.jpg">
</li>
<li class="mySlides1">
<img src="images_2.jpg">
</li>
<li class="mySlides1">
<img src="images_4.jpg">
</li>
</ul>
</div>
<div id="slider2">
<ul>
<li class="mySlides2">
<img src="images_1.jpg">
</li>
<li class="mySlides2">
<img src="images_3.jpg">
</li>
<li class="mySlides2">
<img src="images_2.jpg">
</li>
<li class="mySlides2">
<img src="images_4.jpg">
</li>
</ul>
</div>
I edited the question
As the question has been updated, I'm updating my answer to fit the new requirements. Basically, I've just added a parameter to the _GetSSImages() function. This allows you to pull the images from a specific slideshow, assuming that the class="" is unique for each slideshow. If not, then I would need to adjust the structure of the function.
----Original answer with updated code:
There are few things I don't know, such as how you intend to display the list of images from the slideshow, or how the client will edit this list.
However, you can use the javascript function below to grab all of the image src attributes from the slideshow HTML based on what you've shown here. The function returns an array of all src attributes so you can call it wherever you need to in your code to get the list.
function _GetSSImages(slide) {
var slideEls = document.querySelectorAll(slide),
slideImgs = [];
for(var i = 0; i < slideEls.length; i++) {
if(slideEls[i].querySelector("img").src != null) slideImgs.push(slideEls[i].querySelector("img").src);
}
return slideImgs;
}
var slideShow1 = _GetSSImages("mySlides1"),
slideShow2 = _GetSSImages("mySlides2");
<div id="slider1">
<ul>
<li class="mySlides1">
<img src="images_1.jpg">
</li>
<li class="mySlides1">
<img src="images_3.jpg">
</li>
<li class="mySlides1">
<img src="images_2.jpg">
</li>
<li class="mySlides1">
<img src="images_4.jpg">
</li>
</ul>
</div>
<div id="slider2">
<ul>
<li class="mySlides2">
<img src="images_1.jpg">
</li>
<li class="mySlides2">
<img src="images_3.jpg">
</li>
<li class="mySlides2">
<img src="images_2.jpg">
</li>
<li class="mySlides2">
<img src="images_4.jpg">
</li>
</ul>
</div>

I'm trying to use hamburger menu on bulma css, but it doesn't work. What is wrong?

I'm new on bulma css http://bulma.io/
I'm trying to use hamburger menu for mobile user.
I just followed instruction on this page: http://bulma.io/documentation/components/nav/
But it doesn't work. What should I add ?
Actually, I can see hamburger menu, but it doesn't work when I am click it.
Thank you.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1">
<title>test</title>
<link rel="stylesheet" href="css/bulma.min.css">
<link rel="stylesheet" href="css/custom.css">
</head>
<body>
<section class="hero is-fullheight is-primary is-bold">
<!-- Hero header: will stick at the top -->
<div class="hero-head">
<header class="nav">
<div class="container">
<div class="nav-left">
<a class="nav-item" href="/">
<img src="img/logo.png" alt="Logo">
</a>
</div>
<span class="nav-toggle">
<span></span>
<span></span>
<span></span>
</span>
<div class="nav-right nav-menu">
<a class="nav-item" href="/about">
About
</a>
<a class="nav-item" href="/path">
Path
</a>
<a class="nav-item" href="/blog">
Blog
</a>
</div>
</div>
</header>
</div>
<!-- Hero content: will be in the middle -->
<div class="hero-body">
<div class="container has-text-centered">
</div>
</div>
</section>
</body>
</html>
This solution uses Vue.js to toggle the bulma nav component when viewing on mobile. I hope this helps others that are looking for an alternative solution.
JS
First, I add the cdn for Vue.js which can be found here https://unpkg.com/vue, and include an instance of Vue in the javascript.
new Vue({
el: '#app',
data: {
showNav: false
}
});
HTML
a. Wrap the nav section with the element "app" to make it reactive (this maps to the "el" property of the Vue instance).
<div id="app"> ... </div>
b. Update .navbar-burger using the v-on: directive to listen for the click event and toggle the data property showNav.
<div class="navbar-burger" v-on:click="showNav = !showNav" v-bind:class="{ 'is-active' : showNav }">
c. Update .navbar-menu using the v-bind: directive to reactively update the class attribute with the result of the showNav property.
<div class="navbar-menu" v-bind:class="{ 'is-active' : showNav }">
Solution
I've included the entire solution in this jsfiddle
This may be an issue with the example given in the docs, I was able to get it working by adding ids to the .nav-toggle and .nav-menu.
<span id="nav-toggle" class="nav-toggle"> and <div id='nav-menu' class="nav-right nav-menu">
jsfiddle here.
So to get the example working, you'd have to add 'id's to the respective elements. I'd recommend deep diving into the docs though
(function() {
var burger = document.querySelector('.nav-toggle');
var menu = document.querySelector('.nav-menu');
burger.addEventListener('click', function() {
burger.classList.toggle('is-active');
menu.classList.toggle('is-active');
});
})();
You can get it to work without using jquery or bulma.js. Just simply use your own javascript to add is-active to nav-menu class on click on nav-toggle.
nav-menu is collapsed.
nav-menu is-active is expanded.
I thought someome might be looking for a solution without jquery so there will be everything in one place.
To make the toggle click event work, just add the below js code. You may create a JS folder, then bulma.js file.
// source: https://gist.github.com/Bradcomp/a9ef2ef322a8e8017443b626208999c1
(function () {
var burger = document.querySelector('.burger');
var menu = document.querySelector('#' + burger.dataset.target);
burger.addEventListener('click', function () {
burger.classList.toggle('is-active');
menu.classList.toggle('is-active');
});
})();
At the end of your html page add the script.
ex:
<script async type="text/javascript" src="./js/bulma.js"></script>
There is an easier way! Before I get to that, it looks like there are a few problems with your Html.
First issue. You don't have the navbar structure set up like the documentation suggests. If you replace the nav-left with navbar-brand it will take care of some of your Html for you. In your code example, you have your logo as a nav-item inside of a nav-left. navbar-brand does exactly that. What you've done here may work but, I'm not sure exactly what that would do. From the documentation:
"navbar-brand the left side, always visible, which usually contains the logo and optionally some links or icons"
So if you take that out to the level of the of the container and within that, you can just put your logo inside the first navbar-item. Next thing is to pull the 'navbar-burgerinside of thenavbar-brand`.
"The navbar-burger is a hamburger menu that only appears on mobile. It has to appear as the last child of navbar-brand."
Once that is set up you'll want to put the menu items you wish to collapse inside of the navbar-menu. This container should be a sibling of the navbar-brand so they should be on the same level together. So your code (within your container div) should look something LIKE SO:
<nav class="navbar">
<div class="navbar-brand">
<a class="navbar-item" href="/">
<img src="../assets/icon.png" alt="something">
</a>
<div class="navbar-burger burger" data-target="Options">
<span></span>
<span></span>
<span></span>
</div>
</div>
<div class="navbar-menu" id="Options">
<div class="navbar-end">
<a class="nav-item" href="/about">About</a>
<a class="nav-item" href="/path">Path</a>
<a class="nav-item" href="/blog">Blog</a>
</div>
</div>
</nav>
The final piece to the puzzle is: you gotta set the data-target of your burger to the the id of the navbar-menu. It's not very clear that's what they are talking about in the docs. Whatever the case, after you make those changes, the javascript code from the documentation should work!
I realize that this question was asked many moons ago but I hope this can help SOMEONE.
Bulma Docs
http://bulma.io/documentation/components/navbar/
My simple but functional answer:
function toggleBurger() {
var burger = $('.burger');
var menu = $('.navbar-menu');
burger.toggleClass('is-active');
menu.toggleClass('is-active');
}
And in the burger tag:
<div class="navbar-burger burger" data-target="navMenu" onclick="toggleBurger()">
<span></span>
<span></span>
<span></span>
</div>
Here is an angular solution:
template - button (notice the last two lines)
<a
role="button"
class="navbar-burger burger"
aria-label="menu"
aria-expanded="false"
data-target="navbarBasicExample"
[class.is-active]="showBurgerMenu"
(click)="showBurgerMenu = !showBurgerMenu">
template - menu (notice the last line)
<div
id="navbarBasicExample"
class="navbar-menu"
[class.is-active]="showBurgerMenu">
component (notice the showBurgerMenu property)
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-navbar',
templateUrl: './navbar.component.html',
styleUrls: ['./navbar.component.sass']
})
export class NavbarComponent {
showBurgerMenu: boolean;
constructor() {}
}
The bulma package does not provide any javascript. So you have to write it yourself. But you do not need anything fancy. Just make the id of toggled element and the data-target of the burger be the same. Something like this:
<a role="button" class="navbar-burger" data-target="navMenu" aria-label="menu" aria-expanded="false">
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
<span aria-hidden="true"></span>
</a>
<div class="navbar-menu" id="navMenu">
<!-- navbar-start, navbar-end... -->
</div>
And add this javascript snippet into end of your file(note: you do not have to change anything):
<script>
document.addEventListener('DOMContentLoaded', () => {
// Get all "navbar-burger" elements
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);
// Check if there are any navbar burgers
if ($navbarBurgers.length > 0) {
// Add a click event on each of them
$navbarBurgers.forEach( el => {
el.addEventListener('click', () => {
// Get the target from the "data-target" attribute
const target = el.dataset.target;
const $target = document.getElementById(target);
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
el.classList.toggle('is-active');
$target.classList.toggle('is-active');
});
});
}
});
</script>
And that is it! It works!
These codeblocks has been taken from documentation: https://bulma.io/documentation/components/navbar/#navbar-burger
you can also make it work with an inline onclick
<a role="button" onclick="this.classList.toggle('is-active');document.querySelector('#'+this.dataset.target).classList.toggle('is-active');" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="efpnavbar">
I had the same problem with Vue.js and I solved it like this :
<span class="navbar-burger burger" data-target="navbarMenu" #click="show()">
<span></span>
<span></span>
<span></span>
</span>
</div>
<div id="navbarMenu" class="navbar-menu">
<div class="navbar-end">
<div class="tabs is-right">
<ul>
<li><a>Home</a></li>
<li>Examples</li>
<li>Features</li>
<li>Team</li>
<li>Help</li>
</ul>
</div>
</div>
</div> </span>
then I added this code in methods section :
methods:{
show()
{
var burger = document.querySelector('.burger');
var menu = document.querySelector('.navbar-menu');
burger.classList.toggle('is-active');
menu.classList.toggle('is-active');
}}
I hope this code helps someone .
If you use SCSS and i.e. purge-css for your project, make sure the .is-active class is not stripped out by your css-purging...
For me then the vanilla example worked:
JS:
document.addEventListener("DOMContentLoaded", () => {
// Get all "navbar-burger" elements
const $navbarBurgers = Array.prototype.slice.call(
document.querySelectorAll(".navbar-burger"),
0
);
// Add a click event on each of them
$navbarBurgers.forEach((el) => {
el.addEventListener("click", () => {
// Get the target from the "data-target" attribute
const target = el.dataset.target;
const $target = document.getElementById(target);
// Toggle the "is-active" class on both the "navbar-burger" and the "navbar-menu"
el.classList.toggle("is-active");
$target.classList.toggle("is-active");
});
});
});
and HTML:
<nav class="navbar headnavi is-fixed-top" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="{{ site.url }}">
<span class="is-size-3 is-size-5-mobile has-text-weight-semibold ml-2">{{ site.name }}</span>
</a>
<a role="button" class="navbar-burger" aria-label="menu" aria-expanded="false" data-target="top">
<span aria-hidden="true" class="nav-toggle" ></span>
<span aria-hidden="true" class="nav-toggle" ></span>
<span aria-hidden="true" class="nav-toggle" ></span>
</a>
</div>
<div id="top" class="navbar-menu">
<div class="navbar-start">
</div>
<div class="navbar-end">
<a class="navbar-item">
Home
</a>
<a class="navbar-item">
Documentation
</a>
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link" href="/">
More
</a>
<div class="navbar-dropdown is-right">
<a class="navbar-item">
About
</a>
<a class="navbar-item">
Jobs
</a>
<a class="navbar-item">
Contact
</a>
<hr class="navbar-divider">
<a class="navbar-item">
Report an issue
</a>
</div>
</div>
<div class="navbar-item">
<div class="buttons">
</div>
</div>
</div>
</div>
</nav>

Scraping using codeigniter and i need title and custom link

<div id="archive_content_block">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="archive_cat_caption">
<h4>অর্থনীতি</h4>
</div>
<ul>
<li><i class="fa fa-square-o"></i>
<a href="http://67.227.189.112/~rtvnews24/economy/2126/বাধ্যতামূলক-করারোপের-প্রস্তাব-অর্থমন্ত্রীর">
<font style="color:rgb(33, 33, 33)">বাধ্যতামূলক করারোপের প্রস্তাব অর্থমন্ত্রীর</font>
</a>
</li>
</ul>
</div>
<div class="col-md-6 col-sm-6">
<div class="archive_cat_caption">
<h4>খেলাধুলা</h4>
</div>
<ul>
<li><i class="fa fa-square-o"></i> <font style="color:rgb(33, 33, 33)">অস্ট্রেলিয়া রেকর্ড</font></li>
</ul>
</div>
</div>
</div>
[this is my scraping html page code]
here i have a function which for get scraping title and link. but not work
public function get_dom()
{
$this->load->library('scraping');
$date = date('Y/m/d'); // custom date
$url = "http://67.227.189.112/~rtvnews24/archive/$date"; //my link
$html = file_get_html($url);
$row = $html->find('div.archive_content_block',0); // select content
foreach($row->find('div.archive_cat_caption', 0) as $title) {
echo $title->find('h4', 0)->plaintext.'</br>'; //title
foreach($row->find('ul',0) as $link) {
echo $link->find('li',0)->find('a', 0)->href.'</br>'; //link
}
}
}
here i am try to get title and link. please help me. Thanks in advance

Making sidebar-nav tabs active?

I have a sidebar-nav that has several tabs. How can I make the current tab viewed by users active/highlighted ??
I tried the active class but it seemed to make one tab active constantly!
any help would be greatly appreciated
Here is my view:
<div id="wrapper">
<div id="sidebar-wrapper">
<ul class="sidebar-nav">
<li class="sidebar-brand">
KU Faculty
</li>
<li>
<hr style='margin-top:2px; color:#000;margin-right:30px;'>
</li>
<li>
My Profile
</li>
<li>
Reseasrch
</li>
<li>
Teaching</li>
</li>
<li>
Experience</li>
</li>
<li>
Publications
</li>
<li>
Resume
</li>
<li>
About us
</li>
</ul>
</div>
You can try to match url path with jQuery and add "active" class to it with:
$(function(){
var this_url = window.location.pathname;
var list_sidebar_link = $(".sidebar-nav li a");
$(list_sidebar_link).each(function (i, item) {
var the_a = list_sidebar_link[i];
if (this_url == $(the_a).attr('href')) {
$(the_a).addClass('active');
}
});
});

JQueryMobile 1.1.1 List items not anchoring to the left

I'm trying to create a simple thumbnail list, the code is pretty much copied off jqm docs pages. However, when I use the code below the button element of the list isn't anchored to the left of the list item and instead appears centered...Can anyone help me? It's driving me crazy!
I haven't got any styles in other than what is in the jquerymobile default page template
<div id="listDiv" class="ui-content" data-role="main">
<div id="listInformation" data-role="content-primary">
<ul id="swipeMeChildrenList" data-role="listview" class="ui-listview">
<li data-corners="false" data-shadow="false" data-iconshadow="true" data-wrapperels="div" data-icon="arrow-r" data-iconpos="right" data-theme="c" class="ui-btn ui-btn-icon-right ui-li-has-arrow ui-li ui-btn-up-c">
<div class="ui-btn-inner ui-li">
<div class="ui-btn-text">
<a href="index.html" class="ui-link-inherit">
<img src="images/album-p.jpg" class="ui-li-thumb">
<p>Ha</p>
</a>
</div>
<span class="ui-icon ui-icon-arrow-r ui-icon-shadow"> </span>
</div>
</li>
</ul>
</div>
</div>
What code do you use to generate your list from? A simple list will be specified like this:
<ul data-role="listview">
<li>
<a href="index.html">
<img src="images/album-p.jpg" />Ha
</a>
</li>
</ul>
jQuery Mobile will take care of adding all the necessary classes depending on the platform.
I suggest opening a new document and using the defaults from that page, ie
<ul data-role="listview">
<li><a href="index.html">
<img src="images/album-bb.jpg" />
<h3>Broken Bells</h3>
<p>Broken Bells</p>
</a></li>
</ul>
Then if that works, piece by piece replace the code with yours until it breaks, that should isolate the issue.