I have the following code to display an image inside of a carousel
<div class="carousel container px-0" *ngIf="module.type === 3">
<div class="container px-0 position-absolute left">
<div [ngStyle]="{ 'background-image': 'url(' + module.urlLeft + ')'}" class="img-container" (click)="decrease(i)">
</div>
</div>
<div class="container px-0 position-absolute right">
<div [ngStyle]="{ 'background-image': 'url(' + module.urlRight + ')'}" class="img-container" (click)="increase(i)">
</div>
</div>
<div [ngStyle]="{ 'background-image': 'url(' + module.urlCenter + ')'}" class="img-container">
</div>
</div>
module is an array's object containing several image urls, the array contains multiple modules:
urlLeft: 'url'
urlCenter: 'url'
urlRight: 'url'
How do I switch through all images of this specific module (there might be several modules with carousels), e.g. by assigning the image from urlLeft to urlCenter, image from urlCenter to urlRight & image from urlRight to urlLeft?
I tried the following, but obviously this won't work since as soon as urlLeft is urlCenter, urlRight will also be urlCenter:
decrease(index) {
this.modules[index].urlLeft = this.modules[index].urlCenter;
this.modules[index].urlCenter = this.modules[index].urlRight;
this.modules[index].urlRight = this.modules[index].urlLeft;
}
With ES6 it will be easy , Try this :
const { urlLeft , urlCenter , urlRight } = this.modules[index];
this.modules[index].urlLeft = urlCenter;
this.modules[index].urlCenter = urlRight;
this.modules[index].urlRight = urlLeft;
Working Code Snippet:
var jsonObj = { first : 1 , second : 2 , third : 3 };
console.log( 'Before Exchange ====> ' , jsonObj);
const { first , second , third } = jsonObj;
jsonObj.first = second;
jsonObj.second = third;
jsonObj.third = first;
console.log( 'After Exchange ====> ' ,jsonObj);
You can also try an auxiliary variable to save urlLeft temporarily.
decrease(index) {
const auxVar = this.modules[index].urlLeft;
this.modules[index].urlLeft = this.modules[index].urlCenter;
this.modules[index].urlCenter = this.modules[index].urlRight;
this.modules[index].urlRight = auxVar;
}
Related
Sample HTML Element
<div data-testid="Custom Data Test">
The above div does not have a class inside.
How do I access, "data-testid" (which selector should I use)
You can use querySelector() or querySelectorAll() and select by attribute like this:
const div = document.querySelector('div[data-testid="Custom Data Test"]');
div.textContent = "blah";
const divs = document.querySelectorAll('div[data-testid]');
divs.forEach( (e,i) => e.textContent = e.textContent + " this is div number " + i + " and data-testid attribute is " + e.dataset.testid);
Sample HTML Element
<div data-testid="Custom Data Test"></div>
<div data-testid="Custom Data Test 2"></div>
<div data-testid="Custom Data Test 3"></div>
If I want to replace
1. First title
with
1. First title
which means I want to replace the garbage text with the numbering in the content, how can I do that with command find and replace engine like the ones in VScode or sublime text?
I have tried:
find: #.*">[0-9]+\.
replace: #$$0">$$0.
This doesn't work at all and further, when the list goes long, numberings may have more digits, like "17. Some title". Better use only plain find and replace engines rather than other programmes or JS.
You could do it in jQuery:
$('a').each((i, a) => {
$(a).attr('href', '#' + parseInt($(a).text()));
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
1. First title
2. Second title
3. Third title
or in pure JavaScript:
document.querySelectorAll('a').forEach(a => {
a.href = '#' + parseInt(a.innerHTML);
});
1. First title
2. Second title
3. Third title
You can do it with Vanilla Javascript using a regex matching the first digit of the string :
^[\d]*
You can test it here : https://regex101.com/r/Vq0Vm0/1
Something like that will work :
const $aList = document.querySelectorAll('a');
const pattern = /^[\d]*/g;
$aList.forEach($a => {
const digit = $a.textContent.match(pattern)[0];
$a.setAttribute('href', `#${digit}`);
});
1. First title
17. First title
1456456. First title
1456546654. First title
1546456. First title
I would personally use jQuery alongside a RegEx match to extract the number in the string.
HTML:
<div class="row d-flex justify-content-center">
<div class="col-md-4 d-flex justify-content-center">
<button class="btn-primary btn-change-content">Change content</button>
</div>
<div class="col-md-4 d-flex justify-content-center">
1. First title.
</div>
<div class="col-md-4 d-flex justify-content-center">
2. Second title.
</div>
</div>
jQuery:
$('.btn-change-content').on("click", function() {
$('a').each(function () {
var anchorText = $(this).text();
var num = anchorText.match(/\d+/)[0];
$(this).attr("href", "#"+num);
});
});
$('a').on("click", function(e) {
e.preventDefault();
var hrefAttr = $(this).attr("href");
alert(hrefAttr);
});
Snippet: (try to first click the anchor tags, then change content and click them again)
$('.btn-change-content').on("click", function() {
$('a').each(function () {
var anchorText = $(this).text();
var num = anchorText.match(/\d+/)[0];
$(this).attr("href", "#"+num);
});
});
$('a').on("click", function(e) {
e.preventDefault();
var hrefAttr = $(this).attr("href");
alert(hrefAttr);
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row d-flex justify-content-center">
<div class="col-4 d-flex justify-content-center">
<button class="btn-primary btn-change-content">Change content</button>
</div>
<div class="col-4 d-flex justify-content-center">
1. First title.
</div>
<div class="col-4 d-flex justify-content-center">
2. Second title.
</div>
</div>
Codepen Example here.
I am trying to wrap html around inner div only when some condition is met. But when I run the script, it says Module build failed: SyntaxError Unexpected token. I tried to use conditional rendering.
return (
<Fragment>
<div>
{
(true) ? <div class="imghvr-wrapper"> : ''
}
<div class="imghvr">
<div class="imghvr-overlay imghvr-anim-none imghvr-anim-single">
</div>
{
(true) ? </div> : ''
}
</div>
</Fragment>
);
Like this:
const myComponent = () => {
const myCondition = true;
const child = (
<div className="imghvr">
<div className="imghvr-overlay imghvr-anim-none imghvr-anim-single" />
</div>
);
return (
myCondition ? <div className="imghvr-wrapper">{ child }</div> : child
);
};
You can't split tags like this: myCondition ? <div className="imghvr-wrapper"> : null you should always close your tags. For example that code is valid: (true) ? <div className="imghvr-wrapper"/> : '' because tag is closed
I have a little problem with LightGallery plugin (lightGallery - Git). I use jQueryMobile.
My gallery is on a specific page. When I arrive on this page, it will request a remote server to get some pics.
Then I initialize LightGallery. No problem the first time.
But when I leave the Gallery Page and come back after (there is a new request to server), lightGallery is not running.
No error in the browser, I have my photos displayed but I can't click on it to run LightGallery like I've done the first time.
My Code:
HTML:
<div data-role="page" id="pageGallery" data-theme="a">
<div data-role="content" class="center-body">
<h3 class="nomGroupe"></h3><br/>
<div class="demo-gallery">
<ul id="lightgallery" class="list-unstyled row">
</ul>
</div>
</div>
</div>
Javscript :
$( document ).on( "pagecontainerbeforeshow", function ( event, ui ) {
var activePage = $.mobile.pageContainer.pagecontainer( "getActivePage" );
if (activePage[0].id == "pageGallery" ) {
$('#lightgallery').empty();
$(".groupName").empty().append("group "+localStorage['groupName']);
envoieRequete('http://myServer/', {'idGroup' : localStorage['idGroup'], 'token' : localStorage['token']}, 'post', function(output){
if(output.group.photos.length === 0) {
$("#lightgallery").append('<br/><p>Empty for Group : ' + localStorage['groupName']+'</p>');
}
else {
for(i=0; i<output.group.photos.length; i++) {
$('#lightgallery').append('<li class="col-xs-6 col-sm-4 col-md-3" data-responsive="http://myServer/'+localStorage['token']+'/'+localStorage['idGroup']+'/' + output.group.photos[i].id
+ '" data-src="http://myServer/'+localStorage['token']+'/'+localStorage['idGroup']+'/' + output.group.photos[i].id + '" data-sub-html="<h4>PhotoAlt</H4>"><a href=""><img class="img-responsive"\n\
src="http://myServer/'+localStorage['token']+'/'+localStorage['idGroup']+'/' + output.group.photos[i].id + '"/></a></li>');
}
}
});
}
});
$(document).on("pagecontainershow", function () {
var activePage = $.mobile.pageContainer.pagecontainer("getActivePage");
var activePageId = activePage[0].id;
switch (activePageId) {
case 'pageGallery':
$(document).on("tap", "#lightgallery li", function (){
$('#lightgallery').lightGallery({});
});
}
});
You might be using wrong html markup. you can check the following code for reference. This is sample code.
HTML
<div class="row">
<div class="large-12 columns">
<ul class="small-block-grid-2 medium-block-grid-3" id="lightgallery">
<li> <a class="item" href="img/alchemy_icon1.jpg"><img src="img/alchemy_icon1_th.jpg"></a>
</li>
<li> <a class="item" href="img/chandra1.jpg"><img src="img/chandra1-th.jpg"></a>
</li>
<li> <a class="item" href="img/Fish.jpg"><img src="img/Fish-th.jpg"></a>
</li>
</ul>
</div>
</div>
Javascript
$("#lightgallery").lightGallery({
selector: '.item'
});
Basically I'm using bootstrap CSS with the panels / headers
I have an API going from Twitch.tv's Kraken that grabs 3 streams
My problem is it shows the 3 streams in the one box
http://jsfiddle.net/82wNq/28/
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title"><div id="content1"></div> - <div id="content2"></div></h3>
</div>
<div class="panel-body">
<div id="content3"></div>
</div>
</div>
If anyone could give me any pointers I'd be really greatful
You are adding all elements together in loop.
Re-Create elements & add separately.
DEMO:
http://jsfiddle.net/82wNq/30/show/
CODE:
http://jsfiddle.net/82wNq/30/
function (data) {
var temp = "";
$.each(data.streams, function (index, item) {
temp = temp + "<div class='panel-heading'><h3 class='panel-title'></h3><div id='content1'>" + item.channel.display_name + "</div><div id='content2'>" + item.viewers + "</div></div><div class='panel-body'><div id='content3'><img src='" + item.preview.medium + "'/></div></div>";
});
$("#content").html(temp); }