I am in the middle of building my portfolio. What I would like to do:
I would like to let my user 'ask questions' by clicking a chain of divs, depending on each div clicked, possible options connected to that div will then highlight and when the question has been asked, a answer depending on the specific chain would then appear (what type of answer is not important).
So if I have 5 divs:
<div class="what">What</div>
<div class="would">Would</div>
<div class="like">Like</div>
<div class="you">You</div>
<div class="i">I</div>
And my user clicks on the 'What' div, the 'Would' div would highlight (add a class), if then clicked, the 'You' and 'I' divs would highlight and so forth. This would enable my user to ask a question based on the original word/div clicked and which words I then have chosen to 'connect' to that word/div.
I would then like to be able to use the specific string (of words) to fun a function, append a class or such. So that the question 'What', 'Would', 'You' and 'Like' would, as an example show a choice of 3 sentences to choose from, but the question of 'What', 'Would', 'I' and 'Like' would, as an example show a picture of a hotdog or some sort.
I do know a bit of JavaScript, but a little more jQuery, so I would prefer to work in that language if possible. I know that the question is a little complex and that I have not included any code, but I really did not know how to go around this.
Hope this will help:
<style type="text/css">
.highlighted{
background-color:yellow;
}
.container2 , .container1{
cursor:pointer;
margin-bottom: 3%;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$(".Common").click(function(){
var classes = $(this).attr("data");
$( ".highlighted" ).removeClass( "highlighted" );
$( "."+classes ).addClass( "highlighted" );
});
});
</script>
<div class="container1">
<div data="FRUITS" class="Common">FRUITS</div>
<div data="VEGETABLES" class="Common">VEGETABLES</div>
<div data="ANIMALS" class="Common">ANIMALS</div>
<div data="BIRDS" class="Common">BIRDS</div>
</div>
<div class="container2" >
<div class="BIRDS" >Parrot</div>
<div class="FRUITS" >Apples</div>
<div class="VEGETABLES" >Cucumber</div>
<div class="FRUITS" >Oranges</div>
<div class="VEGETABLES" >Carrot</div>
<div class="ANIMALS" >Lion</div>
<div class="BIRDS" >Dove</div>
<div class="ANIMALS" >Tiger</div>
</div>
I don't understand your question. Maybe like this?
<style type="text/css">
.highlighted{
background-color:yellow;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$(".test").click(function(){
var id = this.id ;
id++;
if(id>5){
id =1;
}
$( ".test" ).removeClass( "highlighted" );
$( "#"+id ).addClass( "highlighted" );
});
});
</script>
<div class="what test" id="1">What</div>
<div class="would test" id="2">Would</div>
<div class="like test" id="3">Like</div>
<div class="you test" id="4">You</div>
<div class="i test" id="5">I</div>
Related
I have a website which contains many divs. Some of them have an attribute data-status.
Example:
<div data-status="read">+-*</div>
<div data-status="unread">123</div>
<div data-status="sticked">xyz</div>
I want to add an onClick event to them. How can I identify these divs in Jquery?
These divs are not siblings! They are at different knots.
I found this example
$("input[value='Hot Fuzz']")
on api.jquery.com, but with it I can find attributes with a value equal to my search only. But in my case, I want to find an attribute with any values. Is it possible?
Simple as the attribute selector you already used "[]" - just, without the value part:
$("[data-status]").on("click", function() {
console.log(this.textContent)
});
<div data-status="read">+-*</div>
<div data-status="unread">123</div>
<div data-status="sticked">xyz</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
The same selector can be used in CSS too - if you'll ever need it:
[data-status] {
color: gold;
}
<div data-status="read">+-*</div>
<div data-status="unread">123</div>
<div data-status="sticked">xyz</div>
I'm trying to build an SPA using jquery. I have some html (nothing special).
I just have a menu and some divs. Depending on which menu item you click, a specific div should be displayed.
I can use .show() and .hide() and make use of id's and data-attributes.
But I was wondering if there are better ways to perform this kind of functionality.
I have simplified your code to the bone to show it easier
Since your ID on the triggers kind of matches your sub-menu, I have used them to target the content. You would put the code in doc ready.
$( "#" + this.id.replace("-tab", "-menu") ) this line simply uses the clicked ID and replaces the -tab with -menu. This is done to work with your current html
var $menu = $('#menu');
var $subMenu = $('.sub-menu');
$('.menu-item').on("click", function() {
$menu.hide();
$( "#" + this.id.replace("-tab", "-menu") ).show();
});
$('.back-btn').on( "click", function() {
// hide all sub-menu, no need to be specific
$subMenu.hide();
$menu.show();
});
.sub-menu {
display: none
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container-fluid" id="menu">
<div id="weed-tab" class="menu-item">
<p class="menu-name">Wiet</p>
</div>
<div id="indica-tab" class="menu-item">
<p class="menu-name">Indica</p>
</div>
</div>
<div class="container-fluid sub-menu" id="weed-menu">weed
<div class="back-btn">
back
</div>
</div>
<div class="container-fluid sub-menu" id="indica-menu">indica
<div class="back-btn">
back
</div>
</div>
I'm quite new in coding, trying to educate myself because i'm interested. So, sorry if it's going to be a bit dumb question or not so specific or not really correct...
On my "practicing site" i'm having some navigation links, which are referring to different innerHTML contents (like different pages). I used the 'onClick' event to make them show up, for example like this:
<div class="nav" onClick="changeNavigation('a')">menu</div>
It works with texts perfectly, but my problem is that i don't know how to make the same with an image. So when i click on the image, i want to be redirected to that innerHTML page, like i did it with the text based button. I tried to do it like these two ways, but none of them worked.
<img src="picture.png" onClick="changeNavigation('a')" />
<div onClick="changeNavigation('a')"><img src="picture.png"></div>
Is it possible to make this with an image and the 'onClick' event? Or how else can i make this work?
By the way this is my script to make innerHTML show up:
<script>
function changeNavigation(id) {
document.getElementById('main').innerHTML = document.getElementById(id).innerHTML
}
</script>
I also tried to add my image an id that says 'main' like in the script this way, but with no result.
<img id="main" onClick="changeNavigation('f')" src="picture.png" />
Can you help me please? I would appreciate any answer, because i already searched about this and i didn't find anything that could've helped solve my problem and i'm really stuck right now.
(Sorry if my english isn't the best, it's not my native language.)
I have updated my answer to what you want. You need to the divs id you want to display as a parameter to the function you use for onclick. A sample is below.
var divs = ["Menu1", "Menu2", "Menu3", "Menu4"];
var visibleDivId = null;
function toggleVisibility(divId) {
if(visibleDivId === divId) {
visibleDivId = null;
} else {
visibleDivId = divId;
}
hideNonVisibleDivs();
}
function hideNonVisibleDivs() {
var i, divId, div;
for(i = 0; i < divs.length; i++) {
divId = divs[i];
div = document.getElementById(divId);
if(visibleDivId === divId) {
div.style.display = "block";
} else {
div.style.display = "none";
}
}
}
.main_div{text-align:center; background: #00C492; padding:20px; width: 400px;}
.inner_div{background: #fff; margin-top:20px; height: 100px;}
.buttons a{font-size: 16px;}
.buttons a:hover{cursor:pointer; font-size: 16px;}
img {cursor:pointer}
<div class="main_div">
<div class="buttons">
<img src="http://www.clker.com/cliparts/J/g/2/D/p/I/one-hi.png" width="50px" onclick="toggleVisibility('Menu1');"> <img src="http://www.clker.com/cliparts/E/x/J/x/m/z/blue-number-two-hi.png" width="50px" onclick="toggleVisibility('Menu2');">
<img src="http://www.clker.com/cliparts/L/H/T/b/g/N/three-md.png" width="50px" onclick="toggleVisibility('Menu3');">
<img src="http://www.clker.com/cliparts/v/G/G/A/D/s/four-md.png" width="50px" onclick="toggleVisibility('Menu4');">
</div>
<div class="inner_div">
<div id="Menu1">I'm container one</div>
<div id="Menu2" style="display: none;">I'm container two</div>
<div id="Menu3" style="display: none;">I'm container three</div>
<div id="Menu4" style="display: none;">I'm container four</div>
</div>
</div>
You can just keep all of the sections as children of #main, and selectively show them when the section button in clicked. E.g.,
HTML
<nav>
<button type="button" data-index=0>Show one</button>
<button type="button" data-index=1>Show two</button>
<button type="button" data-index=2>Show three</button>
</nav>
<main id="main">
<section>One</section>
<section class="hidden">Two</section>
<section class="hidden">Three</section>
</main>
CSS
.hidden {
display: none;
}
JavaScript
const buttons = Array.from(document.querySelectorAll('button'));
const contentBlocks = Array.from(document.querySelectorAll('section'));
function hideSections (arr) {
arr.forEach(a => {
a.classList.add('hidden');
});
}
function showSection (index, sections) {
// Just a basic check, not exhaustive by any stretch
if (index !== undefined && sections !== undefined) {
hideSections(sections);
sections[index].classList.remove('hidden');
}
}
buttons.forEach(button => {
button.addEventListener('click', () => {
const contentBlocks = Array.from(document.querySelectorAll('section'));
const index = button.getAttribute('data-index');
showSection(index, contentBlocks);
});
});
Obviously you'll have to adjust your selectors for your use case, but Here's a pen
Here's a GitHub Gist pointing to some examples I created on JSFiddle based off of your specific use case (Stack Overflow doesn't let me post links to JSFiddle directly without including code here, but it's easier to follow along/experiment entirely in JSFiddle):
https://gist.github.com/andresn/f100386f06ee28e35bd83c62d9219890
More advanced stuff:
Ideally, you'd use what's called event delegation instead of adding an onclick to every anchor (DRY = Don't Repeat Yourself is good to always keep in mind while programming and so is KISS = Keep It Simple Silly). Here is a resource explaining event delegation:
https://davidwalsh.name/event-delegate
You can even take this further by preloading all your images so they load behind the scenes when the user first loads the page:
https://perishablepress.com/3-ways-preload-images-css-javascript-ajax/
I've got three elements with IDs "albums", "about", and "contact", and three links to show/hide them via the toggle() function, with IDs "togglealbums", "toggleabout", and "togglecontact". I only want one of these elements to be able to be seen at any time, so I wrote the following functions:
$('#togglealbums').click(function() {
if( $('#about').is(':visible') ) {
$('#about').toggle(function() {
$('#albums').toggle();
});
} else if( $('#contact').is(':visible') ) {
$('#contact').toggle(function() {
$('#albums').toggle();
});
} else {
$('#albums').toggle();
}
});
$('#toggleabout').click(function() {
if( $('#albums').is(':visible') ) {
$('#albums').toggle(function() {
$('#about').toggle();
});
} else if( $('#contact').is(':visible') ) {
$('#contact').toggle(function() {
$('#about').toggle();
});
} else {
$('#about').toggle();
}
});
$('#togglecontact').click(function() {
if( $('#albums').is(':visible') ) {
$('#albums').toggle(function() {
$('#contact').toggle();
});
} else if( $('#about').is(':visible') ) {
$('#about').toggle(function() {
$('#contact').toggle();
});
} else {
$('#contact').toggle();
}
});
First of all, if these are wildly inefficient or there is an easier way to do this, please let me know.
What I've found is that if none of the three DIVs is visible, clicking one of the toggle links will show/hide the respective div with no animation. However, if one of the DIVs is visible, clicking another toggle link will cause the div to shrink and fade and the new one expands and fades in, which I don't want (at least for now). This can be seen here: http://new.e17.paca.arvixe.com.
Can anyone tell me why this is happening?
Thanks!
EDIT:
Markup is here:
<body>
<div id="main">
<div id="nav">
<div id="menu">
<ul>
<li>Albums</li>
<li>About Me</li>
<li>Contact</li>
</ul>
</div>
</div>
<div id="albums">
Albums go here
</div>
<div id="about">
About info goes here
</div>
<div id="contact">
Contact info goes here
</div>
</div>
</body>
Your shrink-and-fade was happening because, if .toggle() is given a callback function, it assumes you want to animate the toggle instead of just switching it on/off. (According to the docs, as of this writing, that's only supposed to happen when you provide a duration. I've submitted a bug report about this.)
See http://jsfiddle.net/mblase75/byKeP/1/ for a reduced example of this. To solve it, just remove the callbacks and put the same code in the next line of your function.
As for streamlining your code, classes are your friends. HTML:
toggle albums
toggle about
toggle contact
<div class="toggleblock" id="albums">ALBUMS</div>
<div class="toggleblock" id="about">ABOUT</div>
<div class="toggleblock" id="contact">CONTACT</div>
Note the data- attributes, which jQuery will parse and make accessible through the .data() method. This makes it easy to store a unique div ID on the hyperlink itself, which in turn streamlines our JavaScript immensely. JS:
$('.togglelink').on('click',function(e) {
var id = $(this).data('block');
$('#'+id).toggle().siblings('.toggleblock').hide();
});
http://jsfiddle.net/mblase75/byKeP/
on the site I'm working on, I need to show two different HTML snippets depending if another element is activated or not.
Like:
{if "div class="box expandResults" is enabled(or appearing on the page)"}
....HTML....
{else}
....OTHER HTML....
{/if}
How can I do this with Smarty or jQuery? I only have access to the .tpl files.
Thanks :)
Do it with JavaScript. Generate following HTML:
<div id="boxExpandResultsEnabled" style="visibility: hidden">
....HTML...
</div>
<div id="boxExpandResultDisabled" style="visibility: hidden">
....OTHER HTML....
</div>
JavaScript for Prototype would be:
<script>
if($$('div.box expandResult')) {
$('boxExpandResultsEnabled').show();
} else {
$('boxExpandResultsDisabled').show();
};
</script>
Same thing for JQuery:
<script>
if($('div.box.expandResult')) {
$('#boxExpandResultsEnabled').show();
} else {
$('#boxExpandResultsDisabled').show();
};
</script>
Well, you surely have some condition to make div class="box expandResults" appear on the page, so why not use the same one for this other case?
I the div in question is showed/updated via javascript (AJAX or something similar), then you can't do this with smarty alone.