var main = function(){
$('.article').click(function(){
//$('.article').removeClass('current');
$(this).removeClass('current');
$('.description').hide();
$(this).addClass('current');
$(this).children('.description').show();
});
$('.article').dblclick(function(){
//$('.description').hide();
$(this).children('.description').hide();
});
};
$(document).ready(main);
This code is make a table arcodition like this http://www.codecademy.com/courses/web-beginner-en-4hxyb/0/5?content_from=make-an-interactive-website%3Ajquery-events click fullcreen you see that using $(this).removeClass('current') instead $('.article').removeClass('current'). this keyword that reference to the 'article' class why using like that it didn't reference to 'article' class, must using '.artilce'. Can someone explain it, thanks!
here you have a simplified code showing that it works
var main = function(){
$('.article').click(function(){
// below will change all with class article to green
// $('.article').addClass('green')
// with this it only changes the clicked one to green
//$(this).addClass('green');
// below will remove red-class from clicked .article
//$(this).removeClass('red');
//below will remove red-class from all elements with .article
$('.article').removeClass('red');
});
}; $(document).ready(main);
http://jsfiddle.net/tgLckfgz/2/
Related
I apologise if my explanation is hard to understand.
Using This Code:
names.push(newName);
var strName = newName;
var newName = document.createElement('p')
newName.setAttribute('id', newName);
document.body.appendChild(newName);
newName.innerText = strName;
$(newName).css('position','absolute')
$(newName).css('top', y);
$(newName).css('left', x);
updateXY();
The user creates a new div (with a user-inputted name) which works fine.
my problem is that i don't know how to know when one of these user-crated-and-named divs is clicked.
For example:
if the user created 2 divs, 'hello' and 'goodbye' i couldnt just use $('#hello').click(function() {}); etc. Because i wouldnt know that the user would've chose to create the div entitled 'hello'
Furthermore, the array names has all of the names of all of the divs in - if this is any help to anybody. Thankyou, and any help is appreciated
Simply add the event listener to the element as you did when styling it:
$(newName).on("click",function(){
console.log("element "+$(this).text()+" clicked");
});
JSFiddle
Or, with JS's addEventListener():
newName.addEventListener("click",function(){});
JSFiddle
Or, the old way for older browsers, onclick:
newName.onclick = function(){};
JSFiddle
You could do something like this
$(newName).on("click",function(e){
// the variable 'e' is the event of click, if we do e.toElement, we get to know who the element clicked is
var $thisDiv = $(e.toElement);
// do something with $thisDiv
});
After you create a div element, you need to initialize 'click' event on the div
<a class="js-create-div" href="#">Create Div</a>
$(document).ready(function(){
function init(){
var newUserDiv = $(".js-user-div").not(".js-inited");
newUserDiv.on("click",function(){
alert($(this).html());
});
newUserDiv.addClass("js-inited");
}
$(".js-create-div").click(function(){
$("#wrapper").append("<div class='js-user-div'>"+Math.random()+"</div>");
init();
});
});
https://jsfiddle.net/ema0nazs/
I want to create some css card with the data retrieved from json. It iterates fine, but I have a problem with the animation. When the user press a button, the card makes an animation and shows more info. The problem is that the animation works just with the first card. How can I solve it? Thank you.
HERE you can find the full code.
This is the script linked to the info's button:
(function(){
'use strict';
var $mainButton = $(".main-button"),
$closeButton = $(".close-button"),
$buttonWrapper = $(".button-wrapper"),
$ripple = $(".ripple"),
$layer = $(".layered-content");
$mainButton.on("click", function(){
$ripple.addClass("rippling");
$buttonWrapper.addClass("clicked").clearQueue().delay(1500).queue(function(){
$layer.addClass("active");
});
});
$closeButton.on("click", function(){
$buttonWrapper.removeClass("clicked");
$ripple.removeClass("rippling");
$layer.removeClass("active");
});
})();
ok your issue is you not detect good element. i have modify your script.js
$(document).on("click",".main-button", function(){
$(this).find(".ripple").addClass("rippling");
$(this).closest("main").find(".button-wrapper").addClass("clicked").clearQueue().delay(1500).queue(function(){
$(this).closest("main").find(".layered-content").addClass("active");
});
});
please try: http://plnkr.co/edit/qZmi3jJS4WVcN676OSP2
UPDATE for close
Try
$(document).on("click",".close-button", function(){
$(this).closest("main").find(".button-wrapper").removeClass("clicked");
$(this).closest("main").find(".ripple").removeClass("rippling");
$(this).closest("main").find(".layered-content").removeClass("active");
});
link : http://plnkr.co/edit/WKtJUqOwkEGnhb2Zc1HZ
I have this javascript:
$(document).ready(function() {
$('#holder').toggleClass("visible");
$('a.link').click(function(event) {
// Over-rides the link
event.preventDefault();
// Sets the new destination to the href of the link
newLocation = this.href;
color = $(this).data("color");
$('body').css('background-color', color );
$('#holder').css('opacity','0' );
// Delays action
window.setTimeout(function() {
// Redirects to new destination
window.location = newLocation;
}, 250);
});
$('h1').click(function(event) {
$('#holder').toggleClass("visible");
});
});
I want to change the use of class body to something like body.subsection.container. How do I do this ?
Also I want to include more than one class so something like body.subsection.container1, body.subsection.container2.
Any help on this greatly appreciated.
Cheers,
Greg.
Assuming you mean finding an element with class container1 or container2 somewhere in an element with class subsection somewhere in the body element, you would do:
$('body .subsection .container1,body .subsection .container2')
body.subsection.container would find a body tag that itself had class subsection and class container.
This is the pen I'm working on.
If you will see the pen,the first container have a child div that is not showing in the result.The Jquery as follows,
$('.content-canvas').find('div').hide();
$('.content-canvas div:first-child').show();
$('.tab-button span:first-child').addClass('active');
$('.tab-button').find('span').click(function(){
$('.tab-button').find('span').removeClass('active');
var currentclass=$(this).attr('class');
$(this).addClass('active');
$('.content-canvas').find('div').each(function(){
if($(this).attr('class')==currentclass)
{
$('.content-canvas').find('div').hide();
$(this).slideDown(200);
$(this).children().show(200);
}
else
{
$(this).hide();
}
});
});
First line:
$('.content-canvas').find('div').hide();
Change to
$('.content-canvas > div').hide();
Do the same with all the same selectors you used. You only need to hide the direct descendant, not all divs.
Also, I recommend caching this selecotr into a variable:
var elements = $('.content-canvas > div').hide();
...
...
element.each(function() {
...
and so on, so that you don't have to jump into the DOM everytime.
It's because at the beginning of your js code, you hide every div.
Show it by using something like:
$('.content-canvas .content1 div').show();
Or put your 'as' inside a span instead of a div such as:
<span>as</span>
Please check my fiddle
i have a little javascript function that does something when one clicks on the element having onclick that function.
my problem is:
i want that, into this function, to set a font color fot the html element having this function onclick. but i don't suceed. my code:
<script type="text/javascript">
function selecteazaElement(id,stock){
document.addtobasket.idOfSelectedItem.value=id;
var number23=document.addtobasket.number;
number23.options.length=0;
if (stock>=6) stock=6;
for (i=1;i<=stock;i++){
//alert ('id: '+id+'; stock: '+stock);
number23.options[number23.options.length]=new Option(i, i);
}
}
</script>
and how i use it:
<li id = "product_types">
<a href="#" onclick='selecteazaElement(<?= $type->id; ?>,<?= $type->stock_2; ?>);'><?= $type->label; ?></a>
</li>
any suggestions? thanks!
i have added another function (jquery one) that does partially what i need. the new problem is: i want that background color to be set only on the last clicked item, not on all items that i click. code above:
$(document).ready(function() {
$('.product_types > li').click(function() {
$(this)
.css('background-color','#EE178C')
.siblings()
.css('background-color','#ffffff');
});
});
any ideas why?
thanks!
I would suggest
$(document).ready(function() {
$('.product_types > li').click(function() {
$('.product_types > li').css('background-color','#FFFFFF');
$(this).css('background-color','#EE178C');
});
});
Your element could have this code:
<li id = "product_types" onclick="selecteazaElement(this);" <...> </li>
To change the foreground color of that element:
function selecteazaElement(element)
{
element.style.foregroundColor="#SOMECOLOR";
}
If you want to change the background color on only the last element clicked, each element must have a different id. I'd suggest naming each one something like product_types1, product_types2, ..., product_typesN, and so on. Then have a reset function:
function Reset()
{
for (var i = 1; i <= N; i = i + 1)
{
document.getElementById('product_types'+i).style.backgroundColor="#RESETCOLOR";
}
}
When you call your selecteazaElement(this) function, first call the Reset function, then set the new element:
function selecteazaElement(element)
{
Reset();
element.style.backgroundColor="#SOMECOLOR";
}
This way all of the elements that start with product_types followed by a number will be reset to one particular color, and only the element clicked on will have the background changed.
The 'scope' of the function when invoked is the element clicked, so you should be able to just do:
function selecteazaElement(id,stock){
document.addtobasket.idOfSelectedItem.value=id;
var number23 = document.addtobasket.number;
number23.options.length=0;
if (stock>=6){
stock=6;
}
for (var i=1;i<=stock;i++){
//alert ('id: '+id+'; stock: '+stock);
number23.options[number23.options.length]=new Option(i, i);
}
// Alter 'this', which is the clicked element in this case
this.style.backgroundColor = '#000';
}
$(function() {
/*if product_types is a class of element ul the code below
will work otherwise use $('li.product_types') if it's a
class of li elements */
$('.product_types li').click(function() {
//remove this class that causes background change from any other sibling
$('.altBackground').removeClass('altBackground');
//add this class to the clicked element to change background, color etc...
$(this).addClass('altBackground');
});
});
Have your css something like this:
<style type='text/css'>
.altBackground {
background-color:#EE178C;
/* color: your color ;
foo: bar */
}
</style>
Attach a jQuery click event to '#product_types a' that removes a class from the parent of all elements that match that selector; then, add the class that contains the styles you want back to the parent of the element that was just clicked. It's a little heavy handed and can be made more efficient but it works.
I've made an example in jsFiddle: http://jsfiddle.net/jszpila/f6FDF/
try this instead:
//ON PAGE LOAD
$(document).ready(function() {
//SELECT ALL OF THE LIST ITEMS
$('.product_types > li').each(function () {
//FOR EACH OF THE LIST ITEMS BIND A CLICK EVENT
$(this).click(function() {
//GRAB THE CURRENT LIST ITEM, CHANGE IT BG, RESET THE REST
$(this)
.css('background-color','#EE178C')
.siblings()
.css('background-color','transparent');
});
});
});
If I am correct, the problem is that the click event is being binded to all of the list items (li). when one list item is clicked the event is fired on all of the list items.
I added a simple .each() to your code. It will loop through each of the list items and bind a event to each separately.
Cheers,
-Robert Hurst