replace URL with href in ajax - html

Using following code to display text from another page
$.ajax({
url: 'topic/ginger.php',
success: function (data) {
data = $(data).find('div#display');
$('.res h2').html(data);
}
});
<a href="topic/ginger.php">
<div class="res" id="content" style="background-color:rgb(50, 190, 166)">
<h2></h2>
</div>
</a>
now i have two div with different href
<a href="topic/icecre.php">
<div class="res" id="content" style="background-color:rgb(224, 79, 95)">
<h2></h2>
</div>
</a>
<a href="topic/jelly.php">
<div class="res" id="content" style="background-color:rgb(92, 184, 92)">
<h2></h2>
</div>
</a>
As you see <a href="topic/ginger.php"> is url in ajax code, now i have two other div with different href so different url how do i sole this

$('.res').each(function(){
getAjax($(this).find("h2"), $(this).parent().attr('href'));
});
function getAjax(child, link)
{
$.ajax({
url: link,
success: function (data) {
data = $(data).find('div#display');
child.html(data);
}
});
}
Assuming all your divs are in the res class I would think this would do it, loops throught the res class and grabs the h2 inside it and the href of its parent and passes it to your ajax call to do the work. Havent tested it though.
EDITL just saw your newest edit, you cant have multiple items with the ID = "content" so get rid of them and give them each class ="res"

Related

Iterate array inside html

I am developing a product sale page. I have used a jquery plugin for implementing a carousel image.
Thats the library:
https://www.jqueryscript.net/demo/Carousel-Plugin-jQuery-RV-Gallery/
I am using the image statically for testing, with some images from internet. But I have to store the images dynamically, so amb I trying to iterate an ARRAY inside html. That array is html formatted.
I need to replace the shopify links, for the content of the arrayImagesProduct. Each product, it may have one, two, three,.. images. So I need to iterate the array .
I have tried this:
$("#productImages").html(arrayImagesProduct.join(''));
but then the slider it doesnt work. I get this error:
Uncaught TypeError: Cannot read property 'clientWidth' of undefined
at Gallery.countMoveRight (RV-gallery.js:121)
at RV-gallery.js:36
at dispatch (VM1025 jquery-1.12.4.js:5226)
at elemData.handle (VM1025 jquery-1.12.4.js:4878)
function showProduct(productCode){
var arrayImagesProduct = [];
$.post('../ServletJson', {
elem : 'productInfo',
code: productCode,
}, function(data) {
$.each(data,function(t,elem){
/* some stuff */
arrayImagesProduct.push('<img class="gallery-item" src="../loadImgProduct.jsp?idImg='+elem.idImg+'&code=productCode">');
});
$("#productImages").html(arrayImagesProduct.join(''));
});
}
How I can iterate the arrayImagesProduct array inside the html?
Inside here
<div class="gallery" id="productImages">
<!-- iterate arrayImagesProduct -->
<\div>
<body>
<div class="container-fluid">
<div class="row image-gallery-container">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-6 ">
<div class="gallery" id="productImages">
<img class="gallery-item" src="https://cdn.shopify.com/s/files/1/0122/2724/8185/products/0719_2019_Top1_b_ae870642-af0d-4655-811d-d597ed2a8bcc_720x.jpg?v=1574239406">
<img class="gallery-item" src="https://cdn.shopify.com/s/files/1/0122/2724/8185/products/0719_2019_Top1_a_720x.jpg?v=1574239409">
</div>
</div>
</div>
<!-- more content -->
</div>
</body>
Uncaught TypeError: Cannot read property 'clientWidth' of undefined
at Gallery.countMoveRight (RV-gallery.js:121)
at RV-gallery.js:36
at dispatch (VM1025 jquery-1.12.4.js:5226)
at elemData.handle (VM1025 jquery-1.12.4.js:4878)
The carousel is instantiating doing the declaration:
<div class="gallery">
jQuery('.gallery').eq(0).initGallery({
nav: ['<i class="material-icons">keyboard_arrow_left</i>', '<i class="material-icons">keyboard_arrow_right</i>'],
close: "<i class='material-icons'>close</i>",
aspectRatio: 3/2,
showDots: true,
showNavIfOneItem: false,
showNav: false,
arrows: true,
transition: "slide",
transitionTime: 700,
});
jQuery.fn.initGallery = function(options) {
if(!options) {
options={};
}
var galleries=[];
for (let i=0; i<this.length; i++) {
options.gallery = this.eq(i);
galleries[i] = new Gallery(options);
}
};

Access functions of main page from innerhtml

I have two html pages. In the main page, when a link is clicked, the url behind the link should be opened in a small inline window. After searching a lot I found that I could use Innerhtml to load the url into a small div (I also tested iframe and ajax but they did not work in chrome). 1. Is it possible to call a function defined in main page, inside the innerhtml? 2. Inside the main page and innerhtml page I have the same tabs and objects of same name, and one of the tabs is called map. when a button is clicked in innerhtml, I want a point to be added in the map of main page not the map of innerhtml itself.
Here is a part of the code:
<script>
function load_home(url) {
document.getElementById("content").innerHTML = '<object type="text/html"
data = "' + url + '" > < /object>';
}
</script>
<ul class="list-group gn-resultview">
<li class="list-group-item" data-ng-repeat="md in searchResults.records" gn-displayextent-onhover="" gn-zoomto-onclick gn-fix-mdlinks="">
<div class="media-body">
<div draggable id="content"> </div>
<h4>
<input gn-selection-md type="checkbox" ng-model="md['geonet:info'].selected" ng-change="change()" />
<!-- Icon for types -->
<a ng-href="#/metadata/{{md.getUuid()}}" onclick="load_home(this.href);return false;" title="{{md.title || md.defaultTitle}}">
<i class="fa gn-icon-{{md.type[0]}}" title="{{md.type[0] | translate}}"/>
{{(md.title || md.defaultTitle) | characters:80}}</a>
</h4>
<p class="text-justify" dd-text-collapse dd-text-collapse-max-length="350" dd-text-collapse-text="{{md.abstract || md.defaultAbstract}}"></p>
<blockquote ng-if="md.getContacts().resource">{{::md.getContacts().resource}}</blockquote>
</div>
<div>
<gn-links-btn></gn-links-btn>
</div>
</li>
</ul>
In the innerhtml page you can call a function of main page such as "test()" like this:
parent.test()
So as an example:
In the innerhtml url:
<script>
function test1()
{
parent.test();
}
</script>
in the innehtml page:
<button type="button"
class="btn btn-default btn-sm btn-block"
data-ng-show="hasAction(mainType)"
onclick="test1()" >

Meteor #each block issues

I would like to iterate over a Mongo query for the user's collections using the each block.
I have the following html template that should load in different pieces of data from the profile object in the users collection.
To clarify, the users Collection has a services, status and profile object
{{#each profile}}
<div class="profileUser oneDiv">
<div class="profileUserLeft">
<div class="profileUserImage">
<div class="spin"> {{> spinner}} </div>
<img src="{{profile.picturelrg}}" class="profileUserImg">
</div>
<div class="profileUserGraph">
<label for="myChart"><b>Meetup Graph</b>
<br>
<span class="profileMonth"> {{profile.month}} </span>
<br>
<canvas id="myChart"></canvas>
</label>
</div>
</div>
<div class="profileUserRight">
<div class="profileUserName">
<ul>
<li><h1>{{profile.name}}</h1></li>
<li>
<div class="circle" style="background-color: {{online.color}}"></div>
</li>
</ul>
</div>
</div>
</div>
{{/each}}
Here is my helper that sets the query
profile: function() {
return Meteor.users.find({
_id: id
});
}
Currently the page loads in no data.
When I statically query for a property however it works. This is done like so.
profimg: function() {
return Meteor.users.find({
_id: id
}).fetch()[0].profile.picturelrg;
}
How can I be more efficient and use the each block instead of statically searching for each different property utilizing the fetch() method?
each of Blaze takes an array as parameter to loop while find method return a Cursor of MongoDB. What you need to do is fetch the Cursor to return the array
profile: function() {
return Meteor.users.find({
_id: id
}).fetch();
}
However, your logic is not correct. You are finding the profile that matches with the input id, thus the function should be
profile: function() {
return Meteor.users.findOne({
_id: id
});
}
and then you can access the property without the each loop

Load view after data is loaded

I have some trouble. I am using this plugin "angular-masonry" (it's on Github) to dynamically build the grid on the page. When the page loads I get this:
http://joxi.ru/YBQPVP3JTJCwfIgLgbc
Here is my code:
<div class="container" style="width:80%">
<h1 style="text-align: center; margin-bottom: 40px">
Category: {{category.text}}
</h1>
<div>(masonry='' load-images="false")
<div class="masonry-brick" ng-repeat="portal in category.claim_portals" style='width:50%;float:left'>
<div>
<h3>(style='margin-left:30px')
Portal: {{portal.text}}
</h3>
<div class="category-list" ng-repeat="claim in portal.portal_claim" style="margin-bottom:2px">
<div class="claim_sections">
<claimforlist claim="claim"></claimforlist>
</div>
</div>
</div>
</div>
</div>
</div>
But after resizing browser window, everything becomes normal and looks like this:
http://joxi.ru/iBQPVP3JTJCUfLnoqQQ
I think that view loads earlier than JSON data arrives.
Can anyone help and tell me how can I load view after the data has arrived? Or if you know another reason of such an issue, please reply.
Thanks in advance.
You can add a scope boolean variable with value set to false, and change the value to true on your http promise success.
Code sample:
function myController($scope, YourDataServer) {
$scope.dataLoadedSuccessfully = false;
yourDataServer
.query()
.$promise
.then(
function(result) {
$scope.dataLoaded = true; // set the value to true
});
}
HTML would look like:
<div id="loadingBar" ng-show="!dataLoadedSuccessfully">Loading data...</div>
<div id="dataWrapper" ng-show="dataLoadedSuccessfully">
<!-- data goes here -->
</div>

how to use same script for 2 ID on same page?

I have a liking script that get ID from php and insert id to data base then the script shows the result by ID. I use same html code and same script for differents Ids but siledown result open on first div id instead of the ont that i click it:
HTML:
Like
<div id="sn_likebox">
<span id="close">X</span>
<div style="height:13px">
<div id="flash">Loading........</div>
</div>
<div id="sn_like_content">
</div>
</div>
<p>----------------------------------</p><div></div>
<p>-----------------------------------</p><div></div>
Like
<div id="sn_likebox">
<span id="close">X</span>
<div style="height:13px">
<div id="flash">Loading........</div>
</div>
<div id="sn_like_content">
</div>
</div>
Script:
$(".like").click(function()
{
var sta_id=$(this).attr("sta_id");
var username=$(this).attr("username");
var dataString ='sta_id='+ sta_id + '&username='+ username;
$("#sn_likebox").slideDown("slow");
$("#flash").fadeIn("slow");
$.ajax({
type : "POST",
url : us_cfg.url,
data: dataString,
cache: false,
success: function(html)
{
$("#flash").fadeOut("slow");
$("#sn_like_content").html(html);
}
});
});
$(".close").click(function()
{
$("#sn_likebox").slideUp("slow");
});
Demo:
http://jsfiddle.net/Ke5AB/112/
You can't place two elements with the same ID in the DOM. You need to make them unique, then your code will work with them.
You cannot have two identical IDs on the same page. Use a different selector type (such as a class).