While using jQuery Mobile <ul data-role="listview">, I am trying to add some <li>s from JavaScript, but the new <li> is not inheriting the jQuery Mobile styles. Here is the code:
<head>
<script>
function init()
{
msg = "<li> <a href=> New List Item </a></li>";
document.querySelector('#add_item').innerHTML += msg;
}
// call init on load
window.addEventListener('load',init,false);
</script>
</head>
<body>
<div id='main' data-role='page' data-theme='c'>
<div data-role='header'>
<h1>Welcome!</h1>
</div>
<div id='content' data-role='content'>
<ul data-role="listview" id="list_cars">
<div id="add_item">
</div>
<li> List Item 1</li>
<li> List Item 1</li>
</ul>
</div>
<div data-role='footer'>
<h4>Enjoy reading the book ...</h4>
</div>
</div> <!-- End of Min page -->
</body>
First, you're putting your <li> inside a <div> instead of directly inside the <ul>. Try changing your querySelector to...
document.querySelector('#list_cars').innerHTML += msg;
or, if you want the item at the top of the list, use this...
document.querySelector('#list_cars').innerHTML = msg + document.querySelector('#list_cars').innerHTML;
Then, you need to tell jQuery Mobile to update that list view by adding...
$('#list_cars').listview('refresh');
The last section on this jQuery Mobile Documentation page about lists describes this feature.
Working example here: http://jsbin.com/omepob/1/
To apply the properties on the new object, the optimal way is to call jQuery's refresh method for the object:
$("#myobject").listview("refresh");
However, I encourage you to replace this innerHTML call for something more DOM-atic, like:
var myli = document.createElement("li");
myli.appendChild(document.createTextElement("List Item 3"));
BTW, agreed with Travis; better include a "ul" as a parent of "li" instead of using a "div" there...
Related
I'm trying to access the elements that are appended by a library that I use in my code, but I can't access them. I have this line in my html.
<div class="bonds" id="original" style="display:block;"></div>
The library that I use will append some elements in here. So from the DOM inspector, it shows something like this.
<div class="bonds" id="original" style="display:block;">
<!-- append start -->
<div class="FL-main fieldsLinker">
<div class="FL-left">
<select></select>
<ul>
<li data-offset="0"></li>
<li data-offset="1"></li>
<li data-offset="2"></li>
</ul>
</div>
</div>
<!-- append ended -->
</div>
I'm trying to access the <ul></ul> element using the methods below but nothing works.
$('#original').children().children().children();
var original = $('#original').find('ul');
My goal is to append <span class="icon-close"></span> in each of <li></li> element when the page is loaded.
You could use JQuery find method to find the ul and loop over it's children.
Add your script at the end of all scripts. (library scripts).
$('#original').find('ul li').each(function(i) {
$(this).append('<span class="icon-close"></span>');
});
I wanna ask is it possible if we adding div element with jQuery, but I need condition like this:
My code before
<li>
<div class="another"></div>
</li>
And what I want is:
<li>
<div class="new">
<div class="another"></div>
</div>
</li>
If it possible, I hope someone can help me.
You could create the new element, append it wherever you want, then append the elements you want into the new element:
var wrapper = $('<div class="new"/>');
$('li').find('a').first().after(wrapper).nextAll().appendTo(wrapper);
The criteria for what you want is not completely clear, but you can try using wrapAll: $(".2, .3, .another").wrapAll("<div class='new' />");
$(".2, .3, .another").wrapAll("<div class='new' />");
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<li>
<div class="another"></div>
</li>
I've created a webpage that has the following structure:
Main html that uses a side menu;
Secondary html that has the information that I want to show in the main html.
I want to achieve the following. Let's say that the secondary html has a section named intro. I want to link that section to a menu item. So when I press the corresponding button, I want to show in my main html the secondary html starting at the #intro section.
This is my sidebar nav in the main html
<div id="main">
<div class="wrapper">
<!-- LEFT SIDEBAR NAV-->
<aside id="left-sidebar-nav">
<ul id="slide-out" class="side-nav leftside-navigation">
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li class="bold"><a class="collapsible-header waves-effect waves-blue active"><i class="mdi-action-view-carousel"></i> Field</a>
<div class="collapsible-body">
<ul>
<li class="active">Intro
</li>
</ul>
</div>
</li>
</ul>
</li>
</aside>
</div>
This is the section in the 2nd html.
<div class="divider"></div>
<div class="section" id="intro">
<li style="list-style: disc">
some text.
</li>
</div>
</div>
When I press the Intro button in the left side nav, I want to open in my main html the 2nd one at the Intro section.
The reason I want to load the 2nd html in my main one is that It uses different css styles and It ruins my formatting if I merge them.
Any solution?
Thank you!
It can be easily achieved with jQuery:
Here's my suggestion:
Step 1
First of all, make sure you have those sections in your secondary.html file:
<div id="intro">Intro section</div>
<div id="section1">Section 1</div>
<div id="section2">Section 2</div>
<div id="section3">Section 3</div>
An, in main.html, make sure you have an element with id=content. This will be your placeholder. Like this:
<div id="content"></div>
Step 2
Modify your anchors:
point href to a dummy url (#).
add a class so we can catch this with jQuery. I named here btn-load-section.
add data- attributes so we can add some useful data to each anchor, to grab it later. I added here data-url and data-section.
Like this:
<li>Intro
<li>Section 1
<li>Section 2
<li>Section 3
Step 3
At the end of our <body> section (in main.html), you can add this code:
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script>
/*
Executes the script inside the anonymous function
only when the page is loaded
*/
$(document).ready(function() {
/*
select all anchors with 'btn-load-section' class (class = dot before)
and bind a click event
*/
$("a.btn-load-section").on("click", function(e) {
e.preventDefault(); //this cancel the original event: the browser stops here
var url = $(this).data('url'); //get the data-url attribute from the anchor
var section = $(this).data('section'); //get the data-section attribute from the anchor
var contentDiv = $("#content"); //select the div with the ID=content (id = hash before). This will be our target div.
/*
executes the jQuery .load function
It will load the url and search for the correspondent section (load page fragment)
e.g. will call load with "secondary.html #intro" (#intro is our fragment on the secondary.html page).
*/
contentDiv.load(url + " #" + section);
});
});
</script>
As I don't know how familiar you're with jQuery, I added some comments.
The first script tag loads jQuery from a CDN.
You can read more about the jQuery's .load function here. But basically it allows to load page fragments:
The .load() method, unlike $.get(), allows us to specify a portion of
the remote document to be inserted. This is achieved with a special
syntax for the url parameter. If one or more space characters are
included in the string, the portion of the string following the first
space is assumed to be a jQuery selector that determines the content
to be loaded.
This is just a possible approach. Hope it helps!
In an unordered list i have some a's with some href's. When clicked I want some html from an external file written. I cannot use any server side languages since it only gonna be running localy.
The structure of my file is:
<body>
<ul>
<li><a href="#">item1</li>
<li><a href="#">item2</li>
<li><a href="#">item3</li>
<li><a href="#">item4</li>
</ul>
<div id="content">
<!-- when a link is clicked write some html from external file to this spot-->
</div>
</body>
</html>
Thanks in advance
Since it looks like you're trying to load a script, there's a better way to do that, by using jQuery.getScript():
$('#triangle').click(function() {
$.getScript("js/triangle.js");
});
Also, as arieljuod points out, you haven't actually loaded jQuery in your HTML file. You'll need to do that to use it:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
(Or pick your favorite jQuery version and CDN; this is one of many.)
You don't want document.write().
You probably want this instead, depending on where you want the new HTML to go:
$('head').append('<script src="js/square.js"></script>');
You can listen for a click on an item and based on that trigger an ajax call for the appropriate file. Load the contents of that file into your content div within the success callback.
Happy to walk you through sample code live over here: https://sudonow.com/session/525cb34035e089113700000a
Pasting the content of the code editor here:
<body>
<ul>
<li id="item1" class="item"><a href="#">item1</li>
<li id="item3" class="item"><a href="#">item2</li>
<li id="item4" class="item"><a href="#">item3</li>
<li id="item5" class="item"><a href="#">item4</li>
</ul>
<div id="content">
<!-- when a link is clicked write some html from external file to this spot-->
</div>
</body>
</html>
//JS
//convert some file content to html
var genHtmlContent = function(content){
//do something here depending on how data is stored e.g. we could just return an html string from some keyvalue json
return content.htmlContent;
}
//Use javascript to detect a click on a list item and grab the appropriate content
$("item").click(function(event){
var selectedId = event.target.id;
var url = "http://www.mysite.com/" + selectedId + ".json";
$.get(url, function(content) {
var htmlContent = genHtmlContent(content);
$("#content").val(htmlContent);
})
})
//sample json file on server
//item1.json
{htmlContent: "<div>I am Item 1</div>"}
A quick and easy solution will be an iframe. Add as many iframes as you want, each having a different url. Give them a common class and hide them using CSS or jQuery. On clicking a link, prevent default and show the corresponding iframe, like;
<ul>
<li><a href="#" id="item1">item1</li>
</ul>
<div id="content">
<iframe class="iframes" id="iframe1" src="http://www.page1.com"></iframe>
</div>
and in your JavaScript, add this;
$('.iframes').hide();
$('#item1').click(function() {
event.preventDefault();
$('#iframe1').show();
});
I need to add an attribute to the first link of a list of links in a project I'm working on and thinking of a way to do this using JavaScript. Here are the codes below, they are dynamically generated but what I want is to figure out a way to insert id="cover_title" in the first tag of every list of with the sequence of , So that the code looks exactly like below. Any ideas?
<ul id="gallery">
<li id="album">
<div id="album_title">Summer 2012</div>
<div id="photo"><a id="cover_title" href="#"><img src="images/photo1.png"/></a></div>
<div id="photo"><img src="images/photo2.png"/></div>
<div id="photo"><img src="images/photo3.png"/></div>
<div id="photo"><img src="images/photo4.png"/></div>
<div id="photo"><img src="images/photo5.png"/></div>
</li>
<li id="album">
<div id="album_title">Spring 2012</div>
<div id="photo"><a id="cover_title" href="#"><img src="images/photo1.png"/></a></div>
<div id="photo"><img src="images/photo2.png"/></div>
<div id="photo"><img src="images/photo3.png"/></div>
<div id="photo"><img src="images/photo4.png"/></div>
<div id="photo"><img src="images/photo5.png"/></div>
</li>
In the first place a note: id comes from identifier and has to be unique throughout the document. Your code should use the class="" attribute instead. See, e.g., MDN on this:
The ID must be unique in a document, ....
That being said, the below code sets the appropriate class on th <a>-element, assuming that you transformed all id attributes above into class.
var links = document.querySelectorAll( 'div.photo:nth-child(2) a' );
for( var i=0; i<links.length; ++i ) {
links[i].classList.add( 'cover_title' );
}
EDIT
Here is an example fiddle: link.
Vega said it right: don't do dublicate ID, use class. Simple in jQuery
$('.album_title').each(function(){
$(this).find('a:first').addClass('cover_title');
});