Well, i have a $.get method to get data from other page and load it to a div. But even with the .each, only one div is affected.
Here is the HTML:
$(document).ready(function() {
$(".post1").each(function() {
var categlink = $(this).find(".post-area > a").attr("href");
var pAvatar = $(this).find("img.postavatar");
var pTTitle = $(this).find(".topictitle");
var pContent = $(this).find(".postcontent");
var pType = $(this).find("img.post-type");
var pOwnerlink = $(this).find(".p-ownerlink");
var pTime = $(this).find(".post-time");
$.get(categlink, function(data) {
var tLink = $(data).find(".main-content:nth-child(5) tr:first-child a.topictitle").attr("href");
$.get(tLink, function(data) {
var fAvatar = $(data).find(".post:first-child .user-basic-info > a > img").attr("src");
pAvatar.attr("src", fAvatar);
var fTopicTitle = $(data).find(".paged-head.clearfix > h1").text();
pTTitle.text(fTopicTitle);
var fTContent = $(data).find(".post:first-child .entry-content > div:first-child").html();
pContent.html(fTContent);
var fType = $(data).find(".post:first-child .posthead > h2 > img").attr("src");
pType.attr("src", fType);
var fOwner = $(data).find(".post:first-child h4.username > a > span > strong").text();
pOwnerlink.text(fOwner);
var fOwnerlink = $(data).find(".post:first-child h4.username > a").attr("href");
pOwnerlink.attr("href", fOwnerlink);
var fTTime = $(data).find(".post:first-child .posttime").text();
pTime.text(fTTime);
$(".postcontent img").each(function() {
if ($(this).height() > 70) {
$(this).addClass("imgresize");
$('.main-forum').isotope({
itemSelector: '.c-post',
percentPosition: true,
});
}
});
});
});
});
});
<div class="c-post post1">
<div class="post-info">
<img class="postavatar">
<div class="topictitle"></div>
</div>
<div class="postcontent"></div>
<div class="post-info2">
<img class="post-type">
<div class="post-owner">Por
<a class="p-ownerlink"></a>
</div>
<div class="post-time"></div>
<div class="post-area">{catrow.forumrow.FORUM_NAME}
</div>
</div>
</div>
<div class="c-post post2">
<div class="post-info">
<img class="postavatar">
<div class="topictitle"></div>
</div>
<div class="postcontent"></div>
<div class="post-info2">
<img class="post-type">
<div class="post-owner">Por
<a class="p-ownerlink"></a>
</div>
<div class="post-time"></div>
<div class="post-area">{catrow.forumrow.FORUM_NAME}
</div>
</div>
</div>
<div class="c-post post3">
<div class="post-info">
<img class="postavatar">
<div class="topictitle"></div>
</div>
<div class="postcontent"></div>
<div class="post-info2">
<img class="post-type">
<div class="post-owner">Por
<a class="p-ownerlink"></a>
</div>
<div class="post-time"></div>
<div class="post-area">{catrow.forumrow.FORUM_NAME}
</div>
</div>
</div>
Actually, my page has various .post1 divs, but only one of all is affected.
Thanks.
EDIT
So, the problem is: we cant use a $.get inside other $.get. The thing is: every .post1 has a link, that i call tLink. After getting tLink of the .post1, i need to run another $.get method using the tLink value to get info from other page. I've tried that, no sucess:
function loadpostinfo() {
$(".post1").each(function () {
var categlink = $(this).find(".post-area > a").attr("href");
var pAvatar = $(this).find("img.postavatar");
var pTTitle = $(this).find(".topictitle");
var pContent = $(this).find(".postcontent");
var pType = $(this).find("img.post-type");
var pOwnerlink = $(this).find(".p-ownerlink");
var pTime = $(this).find(".post-time");
$.get(categlink, function (data) {
var tLink = $(data).find(".main-content:nth-child(5) tr:first-child a.topictitle").attr("href");
});
if (typeof tLink === "undefined") {
} else {
console.log("checked if tLink is valid");
$.get(tLink, function (data) {
console.log("running tLink get method");
var fAvatar = $(data).find(".post:first-child .user-basic-info > a > img").attr("src");
pAvatar.attr("src", fAvatar);
var fTopicTitle = $(data).find(".paged-head.clearfix > h1").text();
pTTitle.text(fTopicTitle);
var fType = $(data).find(".post:first-child .posthead > h2 > img").attr("src");
pType.attr("src", fType);
var fOwner = $(data).find(".post:first-child h4.username > a > span > strong").text();
pOwnerlink.text(fOwner);
var fOwnerlink = $(data).find(".post:first-child h4.username > a").attr("href");
pOwnerlink.attr("href", fOwnerlink);
var fTTime = $(data).find(".post:first-child .posttime").text();
pTime.text(fTTime);
var fTContent = $(data).find(".post:first-child .entry-content > div:first-child").html();
pContent.html(fTContent);
setTimeout(alignposts, 5000);
});
}
});
}
$(document).ready(function () {
loadpostinfo();
});
How do i proceed?
OH MY GOSH, i'm the dumbest man in Earth!
The problem: only the first .post1 of page has content loaded. Each of then have a categlink url value, wich is used to get the TLink url value inside the categlink page. Then i use the TLink with $.get to load content inside this .post1 div.
The fix: only the categlink page from the first .post1 div actually has a TLink inside to use. The other ones was having no content matching my filters. So, this mess was all about data on server, and not JQuery or HTML.
Sorry for wasting your time, guys!
It's all fixed now, thanks!
Related
I have problem with my code.
I have products on my website, each product has his own <a><h1>CODE</h1></a> and I need to take this CODE and paste it before an image. I need to copy element with has class="loop1" and paste it into another element with class="lop1" and then take another element with class="loop2" and paste into element with class="lop2" and so on..
I made class with same numbers for easier copying, but it doesnt work. Can sombody help me?
This is my code:
$('#loop').addClass(function(i) {
return 'lop'+(i+1);
});
$('.p-name').addClass(function(i) {
return 'loop'+(i+1);
});
function doForm() {
var numb = ["1","2","3","4","5","6","7","8","9","10","11","13","14"];
for (var i=0;i<numb.length;i++) {
number = numb[i];
selector = '.loop' + number;
if ($(selector).length != 0) {
val = $(selector).html();
$('lop' + number).html(val);
}
}
}
doForm();
Related html:
<div class="columns">
<div id="loop" class="lop1"></div>
<div class="p-image">
<img src="https://" width="290" height="218">
</div>
<div class="p-info">
<span itemprop="name">PRODUCT</span>
</div>
<div>
So I need to take from "p-info > a" and paste it into div "lop1". Depends on number in class copy and paste HTML into div with same number.
Change $('lop' + radek).html(val); to $('.lop' + number).html(val);
Notice the . at the beginning of lop, it will create a selector to fetch element based on the class.
$('#loop').addClass(function(i) {
return 'lop'+(i+1);
});
$('.p-name').addClass(function(i) {
return 'loop'+(i+1);
});
function doForm() {
var numb = ["1","2","3","4","5","6","7","8","9","10","11","13","14"];
for (var i=0;i<numb.length;i++) {
var number = numb[i];
var selector = '.loop' + number;
if ($(selector).length != 0) {
var val = $(selector).html();
$('.lop' + number).html(val);
}
}
}
doForm();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="columns">
<div id="loop" class="lop1"></div>
<div class="p-image">
<img src="https://" width="290" height="218">
</div>
<div class="p-info">
<span itemprop="name">PRODUCT</span>
</div>
<div>
I am supposed to build a store for a a javascript assignment. The store has three items and a counter which tallies the total of the items. Each item is updated through a click event which changes the value based on a data attribute defined in the html. It then saves this to cookies and allows us to use what was stored when we get to a checkout page. The cookies store and the totals update, but unfortunately, each time the click event occurs, the image disappears. I have been scouring the code and I cannot see why this is happening. Can anyone help?
$(document).ready(function() {
$("#jeans-line").text(Cookies.get("jeans") || 0)
$("#jeanJacket-line").text(Cookies.get("jeanJacket") || 0)
$("#belt-line").text(Cookies.get("belt") || 0)
$("#total").text(Cookies.get("total") || 0)
//The DOM will be changed to the key value of each cookie or 0
$('.item').click(function() {
itemTotal = parseInt($(this).text())
oneMore = itemTotal + ($(this).data('cost'))
$(this).text(oneMore)
Cookies.set($(this).data('name'), oneMore)
setTotal()
});
// //updating the total cost of the pseudo-items in shopping
function setTotal() {
var jeans = parseInt(Cookies.get("jeans"))
var jeanJacket = parseInt(Cookies.get("jeanJacket"))
var belt = parseInt(Cookies.get("belt"))
Cookies.set("total", (jeans + jeanJacket + belt) || 0)
$("#total").text(Cookies.get("total") || 0)
};
//Enter data and close the modal
var modal = $("#modal-box")
var email_input;
$(".email-submit").click(function(e) {
e.preventDefault();
email_input = $("#email-val").val()
console.log(email_input)
var checkEmail = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (checkEmail.test(email_input)) {
alert("This is a good email!")
Cookies.set("email", email_input)
modal.css("display", "none")
} else {
alert("This is not a valid email address!")
}
});
//closes the model with close click event
$(".close").click(function() {
modal.css("display", "none");
});
}) //closes document.ready
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row">
<div class="col-md-4">
<div class="item" data-cost="200" data-name="jeans">
<img id="jeansIMG" src="images/jeans.jpg">
<h2 class="item" id="jeans-line"></h2>
</div>
</div>
<div class="col-md-4">
<div class="item" data-cost="300" data-name="jeanJacket">
<img id="jeanJacketIMG" src="images/jean_jacket.jpg">
<h2 class="item" id="jeanJacket-line"></h2>
</div>
</div>
<div class="col-md-4">
<div class="item" data-cost="50" data-name="belt">
<img id="beltIMG" src="images/belt.jpg">
<h2 class="item" id="belt-line"></h2>
</div>
</div>
</div>
<!-- closes the bootstrap row -->
<div class="row">
<div class="col-md-4">
</div>
<div class="col-md-4">
<img class="shoppingCart" src="images/shopping_cart.jpg">
<h2 class="totalTitle">The total for these pseudo-products is:</h2>
<h2 id="total"></h2>
</div>
<div class="col-md-4">
</div>
</div>
The reason why images are getting remove is because of this code:
$('.item').click(function () {
itemTotal = parseInt($(this).text())
oneMore = itemTotal + ($(this).data('cost'))
$(this).text(oneMore) // <-- this overrides everything inside div.item
Cookies.set($(this).data('name'), oneMore)
setTotal()
});
Seeing your overall code, I'd suggest to change inner item class to something else (eg. item-total) so it won't be conflicting with the outer item class. After that adjust the javascript code:
$('.item').click(function() {
itemTotal = parseInt($(this).text())
oneMore = itemTotal + ($(this).data('cost'))
$('.item-total', this).text(oneMore) // <!-- change text in item-total
Cookies.set($(this).data('name'), oneMore)
setTotal()
});
You may also want to modify the setTotal to add default value || 0 so that it will calculate the total properly even if there is one or more items left unclicked:
function setTotal() {
var jeans = parseInt(Cookies.get("jeans") || 0)
var jeanJacket = parseInt(Cookies.get("jeanJacket") || 0)
var belt = parseInt(Cookies.get("belt") || 0)
Cookies.set("total", (jeans + jeanJacket + belt) || 0)
$("#total").text(Cookies.get("total") || 0)
};
You can check the simplified demo in https://jsfiddle.net/nm5dL9h1/
I'm trying to create a function where I click a button and I get content from a <div>. After, I need to divide the content. I mean, if this div has 10 children, I need to save 5 children in my var code1 and the other 5 in var code2. My problem is I'm not able to use html() function. My code looks like:
$(".pluscontrol").click(function(){
var id = $(this).attr("id");
var items=$(this).closest("table").parent().next().children('#'+id).children().length;
var middle = items / 2;
var code1="";
$(this).closest("table").parent().next().children('#'+id).html(
function(index,currentcontent){
if (index < middle )
code1 = code1 + currentcontent;
});
if ( $(".modal-body .row .sdiv").attr("id") == 1 )
$(".modal-body .row #1.sdiv").html(code1);
if ( $(".modal-body .row .sdiv").attr("id") == 2 )
$(".modal-body .row #2.sdiv").html("...");
});
As you can see, at first, I get the children lenght but I get all items.
I've checked this reference but it not helps too much
the var items is the number of items
Giving an argument to .html() makes it change the HTML of the selected elements. If you want to get the HTML, just call .html() with no argument.
To get the HTML of all the elements in a collection, use .map() to iterate, then combine them with .join().
var allChildren = $(".parent").children();
var middle = Math.floor(allChildren.length/2);
var code1 = allChildren.slice(0, middle).map(function(index, element) {
return element.innerHTML + " " + index;
}).get().join(' ');
var code2 = allChildren.slice(middle).map(function(index, element) {
return element.innerHTML + " " + (index + middle);
}).get().join(' ');
$("#out1").html(code1);
$("#out2").html(code2);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="parent">
<div class="child">Test1</div>
<div class="child">Test2</div>
<div class="child">Test3</div>
<div class="child">Test4</div>
<div class="child">Test5</div>
<div class="child">Test6</div>
<div class="child">Test7</div>
<div class="child">Test8</div>
<div class="child">Test9</div>
<div class="child">Test10</div>
</div>
First group:
<div id="out1"></div>
Second group:
<div id="out2"></div>
You can use querySelectorAll which gives u a collection of child element. It also has a length function which you can call. If you want you can chain a foreach method to it. I have broken it down for clarity.
var children = document.querySelectorAll('.parent > div');
var midPoint = Math.round(children.length / 2);
var firstHalf = Array.prototype.slice.call(children,0,midPoint);
var secondHalf = Array.prototype.slice.call(children,midPoint);
firstHalf.forEach(function(elm){
console.log(elm.innerHTML);
});
<div class="parent">
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
<div class="child"><p>Test1</p></div>
</div>
I have a basic accordion in jQuery. When the user clicks the question, the hidden answer shows up. Currently, in order for the answer to disappear, the user has to click another question. I would like that to remain the same but I would also like for the answer to disappear if the same question is clicked again.
var allPanels = jQuery('.faq-single .faq-answer-section').hide();
jQuery('.faq-question').click(function() {
var nextAnswer = jQuery(this).next();
jQuery(allPanels).not(nextAnswer).slideUp();
nextAnswer.slideDown();
jQuery(this).find('.faq-answer').show();
return false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="faq-single">
<span class="faq-question"><span class="faq-question-icon"></span>Question?</span>
<span style="display:none;" class="faq-answer-section"><span class="faq-answer-icon"></span><span class="faq-answer">FAQ answer</span></span>
</div>
<div class="faq-single">
<span class="faq-question"><span class="faq-question-icon"></span>Question?</span>
<span style="display:none;" class="faq-answer-section"><span class="faq-answer-icon"></span><span class="faq-answer">FAQ answer</span></span>
</div>
<div class="faq-single">
<span class="faq-question"><span class="faq-question-icon"></span>Question?</span>
<span style="display:none;" class="faq-answer-section"><span class="faq-answer-icon"></span><span class="faq-answer">FAQ answer</span></span>
</div>
var allPanels = jQuery('.faq-single .faq-answer-section').hide();
jQuery('.faq-question').click(function() {
var nextAnswer = jQuery(this).next();
jQuery(allPanels).not(nextAnswer).slideUp();
if (nextAnswer.is(":visible")) { nextAnswer.hide(); } else { nextAnswer.slideDown(); }
return false;
});
This works for me:
var allPanels = jQuery('.faq-single .faq-answer-section').hide();
jQuery('.faq-question').click(function() {
var nextAnswer = jQuery(this).next();
if(jQuery(this).next().is(':visible')){
jQuery(this).next().slideUp();
}else{
jQuery(allPanels).not(nextAnswer).slideUp();
nextAnswer.slideDown();
}
return false;
});
This is working fine.
var allPanels = $('.faq-single .faq-answer-section').hide();
$('.faq-single').click(function() {
if($(this).children('.faq-answer-section').is(':visible')){
$('.faq-single .faq-answer-section').hide();
}else{
$('.faq-single .faq-answer-section').hide();
$(this).children('.faq-answer-section').show("slide");
}
});
This question already has answers here:
How to add a class to a given element?
(28 answers)
Closed 9 years ago.
I need to change the class of each href item depending on its value.
I have this code.
<body onload="myFunction()">
<div class="indi-download">
<div class="pull-left">
<h6 class="file" id="file-display-id">%file_display_name%</h6>
</div>
<div class="pull-right">
<a class="download-link" id="download_link" href="%file_url%">Download</a>
</div>
</div>
</body>
In getting the href item on class download-link, I used this javascript code.
function myFunction()
{
var anchors = document.querySelectorAll('a.download-link');
for (var i = 0; i < anchors.length; i++) {
var url = anchors[i].href;
var splitfile = url.split('.').pop();
if(splitfile=='pdf'){
//class="file" would be class="pdf-file"
}else if(splitfile=="docx"){
//class="file" would be class="docx-file"
}else{
//other file format...
}
}
}
on Inspect Element, Something this kind of output.
Element 1 ---
<div class="indi-download">
<div class="pull-left">
//Changed into pdf-file
<h6 class="pdf-file" id="file-display-id">Sample PDF 1</h6>
</div>
<div class="pull-right">
<a class="download-link" id="download_link" href="http://mysite-
info/download/files/file1.pdf">Download</a>
</div>
</div>
Element 2 ---
<div class="indi-download">
<div class="pull-left">
//Changed into docx-file
<h6 class="docx-file" id="file-display-id">Sample docx 1</h6>
</div>
<div class="pull-right">
<a class="download-link" id="download_link" href="http://mysite-
info/download/files/file2.docx">Download</a>
</div>
</div>
How to achieve this kind of output? Changing the classes that depends on the values on href. Any Idea?
If you can use jQuery, I think something like this should work:
function myFunction()
{
var anchors = document.querySelectorAll('a.download-link');
for (var i = 0; i < anchors.length; i++) {
var url = anchors[i].href;
var splitfile = url.split('.').pop();
if(splitfile=='pdf'){
$(anchors[i]).removeClass('file');
$(anchors[i].addClass('pdf-file');
}else if(splitfile=="docx"){
$(anchors[i]).removeClass('file');
$(anchors[i]).addClass('docx-file');
}else{
//other file format...
}
}
}
The class attribute is mapped to the className property so as not to clash with the ECMCAScript future reserved word class, so you want something like:
anchors[i].className = 'docx-file';.
Applied to your example, you can do something like:
var classNames = {pdf:'pdf-file', docx:'docx-file'};
...
anchors[i].className = classNames[splitfile];
and to accommodate a default:
anchors[i].className = classNames[splitfile] || 'default-class';
just in case splitfile doesn't match one of the expected values. And the entire function is:
function myFunction() {
var anchors = document.querySelectorAll('a.download-link');
var classNames = {pdf:'pdf-file', docx:'docx-file'};
for (var i = 0; i < anchors.length; i++) {
var url = anchors[i].href;
var splitfile = url.split('.').pop();
anchors[i].className = classNames[splitfile] || 'default-class';
}
}