I have a "What's New" page on my website, and I want users to be able to hide and show the post using a button/a href link text. The code has been edited into the post below.
HTML:
<p>Post name</p>
<button class="visi" onclick="toggleVis()">+</button>
<div id="post-container" hidden>
<p class="post-text">Test sample paragraph = srve function private</p>
<img src="error.jpeg" alt="error" style="width:auto;height:200px">
</div>
JavaScript:
function toggleVis() {
var x = document.getElementById("post-container");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
CSS:
.visi {
width:auto;
-webkit-border-radius:10px;
height:auto;
background-color:#79E119;
border:5px solid #FFFFFF;
}
Surround the entire blog post with a <div> element, and use jQuery's .toggle() function to show and hide it when users click on a related link or button.
HTML:
<div id="clickme">
Click here
</div>
<div id="blogpost"><h3>my blog title</h3><p>my blog content</p></div>
Javascript:
$( "#clickme" ).click(function() {
$( "#blogpost" ).toggle( "slow", function() {
// Animation complete.
});
});
Related
I have many image icon on my page which have Tooltip on it and show different text in tooltip. I want to show each tooltips when user click on its image icon not on hover. and also when user click on any other place or area of page tooltip also hide or disappear.
<img style="width:26px; height: 23px;" class="customTooltip" data-toggle="tooltip" data-html="true" title="" src="/images/alert.png" data-original-title="Number Typ and Service Type:">
You can do it using bootstrap
<b-button id="tooltip-target-1">
Click Me
</b-button>
<b-tooltip target="tooltip-target-1" triggers="click">
I am tooltip <b>component</b> content!
</b-tooltip>
No need to reinvent the wheel. You can use third party library like TippyJs:
tippy(".item", {
trigger: 'click',
content: `item info`,
allowHTML: true,
animation: 'fade',
onShow(instance) {
let element = instance.reference;
instance.setContent(element.getAttribute('data-info'));
//console.log(instance);
},
});
img {
margin: 2rem
}
<script src="https://unpkg.com/#popperjs/core#2"></script>
<script src="https://unpkg.com/tippy.js#6"></script>
<img class='item' src="https://picsum.photos/id/4/150/100" data-info="item info 1">
<img class='item' src="https://picsum.photos/id/2/150/100" data-info="item info 2">
My JQuery solution without any other js libs for custom tooltip:
<script>
$(document).ready(function() {
var noHideTooltip = false;
$(document).click(function(e) {
if (!noHideTooltip) {
$('.tooltip-box-show').removeClass('tooltip-box-show');
}
});
$(document).on('click', '.tooltip-box', function(e) {
$('.tooltip-box-show').removeClass('tooltip-box-show');
$(this).addClass('tooltip-box-show');
noHideTooltip = true;
setTimeout(function() { noHideTooltip = false; }, 0);
});
});
</script>
.tooltip-text {display:none; background:#ccc;}
.tooltip-box-show ~ .tooltip-text {display:block;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/4e/OOjs_UI_icon_alert_destructive.svg/120px-OOjs_UI_icon_alert_destructive.svg.png" class="tooltip-box"><span class="tooltip-text">Text here..</span
Just add your css tooltip style
Hi i have code like this:
$(document).ready(function() {
$("#toggle").click(function() {
var elem = $("#toggle").text();
if (elem == "Show more") {
$("#toggle").text("Show less");
$("#text").slideDown();
} else {
$("#toggle").text("Show more");
$("#text").slideUp();
}
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div class="btn-container">
<button id="toggle">Show more</button>
</div>
In show more i would like to add a icon /f078 and in show less /f106.
How to add? I tried by css and html tags but it doesnt work.
I want to change the text of the Show/hide button or spoiler button while on click by users. In default condition the button text is written as Show and when user click the button the button text should replace with Hide and again if the user click on Hide the button text should change to Show. How to do that,please anyone help me and i will be very thankful.
<button title="Click to Show/Hide Content" type="button" onclick="if(document.getElementById('spoiler') .style.display=='none') {document.getElementById('spoiler') .style.display=''}else{document.getElementById('spoiler') .style.display='none'}">Show/Hide</button>
<div id="spoiler" style="display:none">
ADD YOUR HIDDEN CONTENT HERE
</div>
You can use pure JavaScript or Jquery to handle this.
In Pure Javascript:
You can define a method and check the element style property. and also style.display is attribute that you should check.
The below code
solve your problem:
<script>
function toggle(){
var spolier = document.getElementById('spoiler');
if(spolier.style.display == "none"){
spolier.style.display= "block";
}
else {
spolier.style.display = "none";
}
}
</script>
<button title="Click to Show" type="button" onclick="toggle()">Show</button>
<div id="spoiler" style="display:none">
ADD YOUR HIDDEN CONTENT HERE
</div>
In Jquery:
You should check attr attribute in jquery.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
function toggle(){
if ($('#spolier').attr("display") == "none"){
$('#spolier').attr('display', 'block');
} else{
$('#spolier').attr('display', 'none');
}
}
</script>
<button title="Click to Show" type="button" onclick="toggle()">Show</button>
<div id="spoiler" style="display:none">
ADD YOUR HIDDEN CONTENT HERE
</div>
Without jquery:
document.querySelector('button').addEventListener('click', toggle)
function toggle(event) {
if (document.getElementById('spoiler').style.display == 'none') {
event.target.innerText = 'Hide'
document.getElementById('spoiler').style.display = ''
} else {
event.target.innerText = 'Show'
document.getElementById('spoiler').style.display = 'none'
}
}
<button title="Click to Show/Hide Content" type="button">Show</button>
<div id="spoiler" style="display:none">
ADD YOUR HIDDEN CONTENT HERE
</div>
You can do it like this...
$(function(){
var button = true;
$('button').on('click', function(){
if ($('button').text().indexOf('Show') > -1){
$(this).text('Hide');
} else{
$(this).text('Show');
}
});
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button title="Click to Show/Hide Content" type="button" onclick="if(document.getElementById('spoiler') .style.display=='none') {document.getElementById('spoiler') .style.display=''}else{document.getElementById('spoiler') .style.display='none'}">Show</button>
<div id="spoiler" style="display:none">
ADD YOUR HIDDEN CONTENT HERE
</div>
How would you make it when a button is clicked the image that the button is made of will change?
#button {
text-align:center;
float:left;
}
#button:focus {
text-align:center;
float:left;
background-image: url('qmb2.png');
}
<input type="image" src="qmb.png" name="saveForm" class="btTxt submit" id="button" height="49.5px" padding = "0px" />
Using jQuery
$("#flowers").click(function () {
var _this = $(this);
var current = _this.attr("src");
var swap = _this.attr("data-swap");
_this.attr('src', swap).attr("data-swap", current);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<img id='flowers' class="full" src='http://www.rachelgallen.com/images/snowdrops.jpg' width="500" height="400" data-swap='http://www.rachelgallen.com/images/daisies.jpg' width="500" height="400" />
Using Javascript
img{height:400px; width:500px}
<script>
function swap()
{
if (document.pic.src=='http://www.rachelgallen.com/images/snowdrops.jpg'){
document.pic.src='http://www.rachelgallen.com/images/daisies.jpg';
}
else if (document.pic.src=='http://www.rachelgallen.com/images/daisies.jpg'){
document.pic.src='http://www.rachelgallen.com/images/snowdrops.jpg';
}
}
</script>
<img src="http://www.rachelgallen.com/images/snowdrops.jpg" name="pic" onclick="swap()"/>
Using pure mouseover/mouseout
img{height:400px; width:500px}
<img src="http://www.rachelgallen.com/images/daisies.jpg"
onMouseOver="this.src='http://www.rachelgallen.com/images/snowdrops.jpg';"
onMouseOut="this.src='http://www.rachelgallen.com/images/daisies.jpg';">
Here is pure JavaScript, if JQuery is out of the question...
<input type="image" id="button" name="img" src="firefox.ico" width="50" />
<script>
document.getElementById("button").addEventListener("click", function(){
this.setAttribute("src", "chrome.ico");
});
</script>
Not that I have something against Rachel's answer on pure JavaScript use, but I just feel there could be something more reader-friendly written to toggle the image, such as:
document.getElementById("button").addEventListener("click", function(){
var path;
if(this.getAttribute("src") === "firefox.ico")
path = "chrome.ico";
else
path = "firefox.ico";
this.setAttribute("src", path);
});
IMO is a little bit cleaner-ish, and it's really not advised to use onclick properties in html, but rather implement listeners via JavaScript.
Jquery, when you click first time the class cliked will be added.
$(document).ready(function(){
$('button').on('click', function(){
$(this).toggleClass('clicked');
});
});
And css:
Button{
Background: img1.jpg;
}
Button.clicked{
Background: img2.jpg;
}
I have used a Masonry view plugin but when the images load through the ajax call, they seem to be displayed one below another (like a list)
HTML
<div id="masonryView" data-ng-repeat="image in images">
<div class="item">
<a target="_blank" href="">
<img src="{{image.imageSourceURL}}" alt="" width="250">
</a>
<div class="desc">{image.description}</div>
</div>
</div>
Ajax
$(window).load(function(){
var $container = $('#masonryView').masonry();
$container.imagesLoaded( function() {
$container.masonry();
});
});
CSS
#masonryView {
position: relative;
}
.item {
margin: 10px;
width: 100px;
}
Does there seem to be a problem that could explain why the images are displayed one below another?
I'm not seeing any AJAX in your posted code labeled "AJAX" but you should call masonry in this way, by setting your itemSelector:
$(window).load(function(){
var $container = $('#masonryView').masonry();
$container.imagesLoaded( function() {
$container.masonry({itemSelector: '.item'});
});
});