how to change title and description on Next and Previous button? - javascript

EDIT: This question is apparently related to the jQuery Carousel Evolution plugin's UI.
How would I change the title and description on next and previous button?
Also, when i load the page and when carousal will display that time 1st title and description should display bydefault. Please solve my problem. I am shared the link also , please see below
$(document).ready(function () {});
var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/0155DF7A9B0DF819?v=2&alt=json&callback=?';
var videoURL = 'http://www.youtube.com/watch?v=';
$.getJSON(playListURL, function (data) {
var list_data = "";
$.each(data.feed.entry, function (i, item) {
var feedTitle = item.title.$t;
var feedescription = item.media$group.media$description.$t;
var feedURL = item.link[1].href;
var fragments = feedURL.split("/");
var videoID = fragments[fragments.length - 2];
var url = videoURL + videoID;
var thumb = "http://img.youtube.com/vi/" + videoID + "/hqdefault.jpg";
list_data += '<div><a href= "#" ><img src="' + thumb + '" width="213px" height="141px"/></a></div>';
});
$(list_data).appendTo(".slides");
$('.carousel').carousel({
carouselWidth: 930,
carouselHeight: 330,
directionNav: true,
shadow: true,
buttonNav: 'bullets'
});
var cnt = 0;
$(".nextButton").click(function () {
var len = $(this).siblings(".slides").children(".slideItem").length;
var a = cnt++;
});
$(".prevButton").click(function () {
var len = $(this).siblings(".slides").children(".slideItem").length;
var a = cnt++;
});
});
Demo Link :- http://jsfiddle.net/4HSpH/4
Thanx in Advance ...

For question 1, you can use jQuery to add your title attributes after carousel initialization. The plugin you've selected doesn't seem to have a very robust API for that sort of thing.
http://jsfiddle.net/4HSpH/6
$('.prevButton').attr('title', 'My previous button');
$('.nextButton').attr('title', 'My next button');
To get your title to populate on load, try this after the code above:
$("#title").text($items.filter(function () {
return $(this).css("top") == "0px";
}).find("a").attr("title"));

Related

dynamic list in javascript how to call function from specific <li>

I get the message list from the API and create a dynamic array using javascript. I would like a new page with message details to be started when a specific row is pressed.
How do I implement a call to showMessage () on a specific table row?
var list = document.getElementById("listOfMessage");
init();
function init() {
for (var i = 0; i < messageList.length; i++) {
var message = messageList[i];
var li = document.createElement("li");
var a = document.createElement("a");
var text = document.createTextNode("Nadawca: " + message.fullName);
a.appendChild(text);
a.setAttribute('onclick', showMessage(message));
list.appendChild(li);
//list.innerHTML += "<li><a href="showMessage(message)"><h2>Nadawca: " + message.fullName + "
//</h2></a></li>";
}
//list = document.getElementById("listOfTask");
}
function showMessage(message) {
window.sessionStorage.setItem("message", JSON.stringify(message));
window.location.href = 'message.html';
}
In the code above, the showMessage () function is immediately called when the array is initialized. How to make it run only after clicking on a row?
I could add an id attribute to the (a) or (li) element in the init () function, but how to find it later and use it in this code:
var a = document.getElementById('1');
a.addEventListener('click', function() {
window.sessionStorage.setItem("message", JSON.stringify(messageList[0]));
window.location.href = 'message.html';
});
I found a way to solve this problem.
Using this code fragment, we can call a function for a specific element in a dynamically created list.
function init() {
for (var i = 0; i < messageList.length; i++) {
var message = messageList[i];
list.innerHTML += "<li id="+i+"><a onClick="+
"><h2>Nadawca: " + message.fullName + "</h2></a></li>";
}
//$(document).on("click", "ui-content", function(){ alert("hi"); });
$(document).ready(function() {
$(document).on('click', 'ul>li', function() {
var idName = $(this).attr('id');
showMessage(messageList[idName]);
});
});
}

button onclick not working on first click

my button.onclick doesn't work on the first click, but it works on the second.
I used an alert to check and even the alert doesnt work on the first click, but works on the second click.
here's the link to the app in case you need it -
http://silentarrowz.imad.hasura-app.io/news
could you tell me what's wrong??
here's the code
window.onclick = function () {
var displayNews = document.getElementById('currentNews');
var newsButton = document.getElementById('getnews');
newsButton.onclick = function () {
alert('the button is clicked');
var newsxr = new XMLHttpRequest();
newsxr.onreadystatechange = function () {
if (newsxr.readyState === XMLHttpRequest.DONE && newsxr.status === 200) {
var currentNews = JSON.parse(newsxr.responseText);
var currentArticles = currentNews['articles'];
var numberArticles = currentNews['articles'].length;
var newsDisplay = '';
var author;
var title;
var description;
var urlToImage;
for (var i = 0; i < numberArticles; i++) {
author = currentArticles[i]['author'];
title = currentArticles[i]['title'];
description = currentArticles[i]['description'];
urlToImage = currentArticles[i]['urlToImage'];
newsDisplay = newsDisplay + "<p>" + "<span class='title'>" + title + "</span>" + "<br>" + description + "<br>" + "<img src='" + urlToImage +
"'</img>" + "</p>";
}
alert('displaying the news now');
console.log('current news is : ', currentNews);
displayNews.innerHTML = newsDisplay;
}
}; //on state change
newsxr.open('GET', 'https://newsapi.org/v1/articles?source=national-geographic&sortBy=top&apiKey=1af110441a8e4f72925f78344e58c2a4', true);
newsxr.send(null);
}; //button onclick function ends
}; // window onclick function ends
The truth about your code is the following..
You are assigning an onclick event to the window. when you click the window it then gets the buttons id which then assigns an onclick event to your button.
Your button only works when you click anywhere in the window (your browser User interface). You can try it and see
SOLUTION
remove the on window.onclick event stuff.
this should be the only code you should be seeing in your editor to make things work.
var displayNews = document.getElementById('currentNews');
var newsButton = document.getElementById('getnews');
newsButton.onclick = function(){
alert('the button is clicked');
var newsxr = new XMLHttpRequest();
newsxr.onreadystatechange = function(){
if(newsxr.readyState ===XMLHttpRequest.DONE && newsxr.status ===200){
var currentNews = JSON.parse(newsxr.responseText);
var currentArticles = currentNews['articles'];
var numberArticles = currentNews['articles'].length;
var newsDisplay ='';
var author;
var title;
var description;
var urlToImage;
for(var i=0;i<numberArticles;i++){
author = currentArticles[i]['author'];
title = currentArticles[i]['title'];
description = currentArticles[i]['description'];
urlToImage = currentArticles[i]['urlToImage'];
newsDisplay = newsDisplay + "<p>"+"<span class='title'>"+ title+"</span>"+ "<br>"+description+"<br>"+"<img src='"+urlToImage+"'</img>"+"</p>";
}
alert('displaying the news now');
console.log('current news is : ',currentNews);
displayNews.innerHTML = newsDisplay;
}
};//on state change
newsxr.open('GET','https://newsapi.org/v1/articles?source=national-geographic&sortBy=top&apiKey=1af110441a8e4f72925f78344e58c2a4',true);
newsxr.send(null);
};
//button onclick function ends
I hope this was explanatory
Because the first click is window.onclick = function() part which tells the window to define another click event only, and then the real click event will work when you click the second time.
Deleting the window click event already suffices.
P.S. I don't see why having window click event is meaningful in your code.

How to add a (1) text notficiation on document title

Hi I've read here about browser tab notifications
This is the code suggested to achieve an (1) on the browser tab every second.
var count = 0;
var title = document.title;
function changeTitle() {
count++;
var newTitle = '(' + count + ') ' + title;
document.title = newTitle;
}
function newUpdate() {
update = setInterval(changeTitle, 1000);
}
var docBody = document.getElementById('site-body');
docBody.onload = newUpdate;
I've tried it and do not seem to work. Can't see why.. Input?
DEMO
http://tutsplus.github.io/tab-notification/index.html
If it's like in the example, script loaded within the body tags, try this one:
var count = 0;
var title = document.title;
var update;
function changeTitle() {
count++;
var newTitle = '(' + count + ') ' + title;
document.title = newTitle;
}
(function() {
update = setInterval(changeTitle, 1000);
})();
Also in your code variable update is undeclared. And you're not using it, so try delete "update".
don't use element.onload because it's still doesn't have (load) that Id when you are run code,check only
if(docBody) newUpdate();

How to insert hyperlink into text using jQuery?

I'd like to make an Insert Link button. Here is what I came up with:
$(document).ready(function(){
function HyperLink(elementID, openTag, closeTag) {
var textArea = $('#' + elementID);
var len = textArea.val().length;
var start = textArea[0].selectionStart;
var end = textArea[0].selectionEnd;
var selectedText = textArea.val().substring(start, end);
var replacement = openTag + selectedText + closeTag;
textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}
$('#btnHyperlink').click(function() {
HyperLink("id_description", "<a>", "</a>");
});
});
The snippent above embraces text with <a>...</a>, however I don't know how to insert href to the text.
Appreciate your help.
Note:
I want links to be dynamically added to the text, as this button is going to be used in a simple text editor.
Then how about:
$('#btnHyperlink').click(function() {
var href = prompt("Insert url");
if(href != "" && href != null)
{
HyperLink("id_description", href);
}
});
And the function:
function HyperLink(elementID, url) {
var textArea = $('#' + elementID);
var len = textArea.val().length;
var start = textArea[0].selectionStart;
var end = textArea[0].selectionEnd;
var selectedText = textArea.val().substring(start, end);
var replacement = $('<div/>').append(
$('<a/>').attr('href', url).html(selectedText)
).html();
textArea.val(textArea.val().substring(0, start) + replacement + textArea.val().substring(end, len));
}
EDIT
Seems that concatenating an a tag into a string gives just the url, so I wrapped it into a div and extracted its content to output it as a string.
EDIT 2
Seems that I copied a wrong version of the code. Sorry. Here's the JSFiddle:
http://jsfiddle.net/rok1ev0g/
You can try like this:
$('#id_description').html('Google');
JSFIDDLE DEMO
Not sure if this is what you are looking for but here it goes:
var field = "http://www.google.com"
$(document).ready(function () {
$('.id_description').each(function () {
this.innerHTML += ' Google';
});
});
JSFIDDLE DEMO
You can give something a href by doing this in JavaScript:
element.href = "url";
In jQuery you can use:
element.attr('href', 'url');
try:
<span class="linkable">Click to insert link</span>
$('.linkable').click(function(){
var text = $(this).html();
var link = $("<a>", {
href: "http://google.com"
}).html(text);
$(this).replaceWith(link);
});
JSFiddle
Try using jQuery wrap()
$( "#id_description" ).wrap( '' );
Maybe this?
$("textarea").on("select", function(){
var start = this.selectionStart;
var end = this.selectionEnd;
var text = this.innerHTML.substring(start, end);
var link = $("#user_link").val();
var new_link ="<a href='"+link+"' target='_blank'>"+text+"</a>";
$(this).after(new_link);
});

Searching an external URL for files with javascript

I am building a page that needs to be able to get a all the file links on a webpage and add them to a dropdown list. Original it was the script was supposed to be on the same page as the files but now it needs to search an external. This is what I used before the change
<script>
$(document).ready(function() {
var arr = [];
var filenames = [];
var alt_var;
var baseURL = "www.fakeurl.com"
$('.ms-vb-icon').find('a').each(function(){
var temp = $(this).attr('href')
$(this).find('img').each(function(){
alt_var = $(this).attr('alt');
});
if(temp.indexOf('.csv') != -1){arr.push(temp); filenames.push(alt_var);}
});
for(i = 0; i < arr.length; ++i)
{
var x = document.createElement('li');
var a = document.createElement('a');
var t = document.createTextNode(" " + filenames[i]);
var fullURL = baseURL + arr[i];
a.setAttribute('href',"#");
a.setAttribute('class', "glyphicon glyphicon-file");
a.setAttribute('id', baseURL + arr[i]);
a.setAttribute('onclick', "drawChart(this.id)");
a.appendChild(t);
x.appendChild(a);
document.getElementById("dropdownfiles").appendChild(x);
}
});
</script>
How can I change this to search an external url. (PS new to Javascript)
Not sure if this is the cleanest way but you could add a hidden iframe on the page and then search in there.
css:
.externalSearcher iframe {
display: none;
}
html:
<div class="externalSearcher"></div>
js:
$('.externalSearcher').append('<iframe src="' + externalLink + '"></iframe>');
$('.externalSearcher').find('a').each(function () {
//do what you want with the link
});

Categories

Resources