Change Class of Multiple Elements at Once - php

I'm standing in front of a forest. Can you help me find the tree?
Currently I'm generating a list with PHP
echo '<div>';
echo ' <ul>';
foreach ( $myArray as $key => $value):
echo '<li>'. $value . '</li>';
endforeach;
echo ' </ul>';
echo '</div>';
The array contains a limited number of items, which can vary.
What I want to do is to highlight the current item and show all other items as normal.
So, when showing the page, the first item should be shown as being highlighted.
When clicking on second item,
the first item should not stick out (not have any styling).
the second item should be highlighted until another item is clicked.
all other items should be shown as a normal styling.
I would like to be able to do this using nothing but CSS.
To be named, when clicking on a list item I plan to show/hide other div elements, where I plan to use jQuery.

$('li').on('click', function() {
$('li.highlight').removClass('highlight'); // first un-highlight all lis
$(this).addClass('highlight'); // then highlight only clicked li
});
To set first item as highlight when page load, you can do it in PHP/ jQuery also.
If you want to do in jQuery then try:
$(document).ready(function() {
$('div > ul > li:first').addClass('highlight'); // set first item
// highlight at page load
$('li').on('click', function() {
$('li.highlight').removClass('highlight'); // first un-highlight all lis
$(this).addClass('highlight'); // then highlight only clicked li
});
});

Try .addClass() .removeClass() and .eq():
$(function(){
$('div ul li').eq(1).addClass('active');
$('div ul li').click(function(){
$(this).siblings().removeClass('active');
$(this).addClass('active');
});
});
As .eq() is 0 indexed so .eq(1) will select the second li on page load.

You probably need to add a class name to the li's to give you a convenient selector. In my example below, I have use some_class for this purpose. I am also assuming that you want to use a class the indicate the selected item. In this example, I have used selected as the class name for this.
$('li.some_class').click(function() {
$('li.some_class').removeClass('selected');
$(this).addClass('selected');
});

thanks all to your fast reply.
I finally mix your answers to get it working for me
$(document).ready(function()
{
$('div#myclass > ul.myid > li:first').addClass('current'); // set first item
$('div#myclass > ul.myid > li').click(function()
{
$(this).siblings().removeClass('current');
$(this).addClass('current');
});
});
The myclass and myid I've added to distinguish between other list items.
#thecodeparadox: The update when clicking another item was not done with your proposal.
#jai: the initial set was not done with your proposal.
But with the final mix, it works 100% as I need.
Thanks a lot again.
Wolfgang

Related

jquery not operator : not able to hide div

I am new to jquery. I am trying to hide getLocation div, if clicked on someother id.
But this doesn't work
$(:not("#getLocation")).click(function(){
if($("#getLocation").show()==true){
$("#getLocation").hide();
}
});
Try this:
$('*:not("#getLocation")').click(function(){
$("#getLocation").toggle();
});
syntax error only.
$('*:not("#getLocation")') => will return all element except whose id = getLocation
Try this Code
$("#getLocation").click(function(){
$("#getLocation").addClass("hide");
});
Use this :
$('#getLocation').on('click', function(){
var target = $(this).attr('rel');
$("#getLocation"+target).show().siblings("div").hide();
});
You can use event.target to filter out relevant div only like following :
<div id="a">
Div 1
</div>
<div id="b">
Div 2
</div>
<div id="getLocation">
Hide/show
</div>
Js
// as example, we target all div
$('div').click(function(e){
// but only filter div without getLocation ID
if ( $(e.target).attr('id') != 'getLocation' ) {
$("#getLocation").toggle();
}
});
DEMO
You may need this.
$(document).not($("#getLocation")[0]).click(function(){
if($("#getLocation").is(':visible')){
$("#getLocation").hide();
}
});
Your statement
if($("#getLocation").show()==true){
Will never true, because it is an object.
jQuery returns an object of the element after every getter operation.
It is object chaining.
For example, if you have an element with id myDiv
Then you can perform three operations in a single statement like this:
$("#myDiv").show().css('color', 'red').css('padding', '12px 12px 12px 12px');
Here, after you show() element, an object of the element is returned.
Again you change the css and again an object is returned.
Use jQuery .toggle()
Corrected code:
$(':not(#getLocation)').click(function(){
$("#getLocation").toggle();
});
You can use container logic like normally use for navigation menu. For example if you click on outside of your div then your div goes hide.
Try this:
$(document).click(function (e)
{
var container = $("YOUR CONTAINER SELECTOR");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{
container.hide();
}
});
Hope this help you well!

Hide the all other divs except one which is clicked

I have list of user and their dates in PHP and onclick of specific user, I want to hide div other than one whose link is clicked
For example :
<div class='person' id='add_new_date_8' style='display:none;'>
<div class='person' id='add_new_date_9' style='display:none;'>
<div class='person' id='add_new_date_10' style='display:none;'>
<a href='javascript:void(0)' onclick="addMydate('<?PHP echo $id;?>')">Add a new Date?<a/>
So if $id = 8 then hide all div of class person which has id other than 'add_new_date_8'
Make it simple in jquery
$(document).on('click', '.person', function() {
$('.person').hide();
$(this).show();
});
access id in js function. and first hide all the div with person class and then show the which have matching id.
function addMydate(id){
$('.person').hide();
$('#add_new_date_'+id).show();
}
The other solutions so far will work, however I prefer to use .siblings()
function addMydate(id){
var selectedEl = $('#add_new_date'+id);
selectedEl.siblings().hide(); //siblings() takes all other elements of the same parent
selectedEl.show();
}
This will prevent the element itself from being hidden and then shown again, and might save you some headaches on animations, should you add those.
note: this depends on your HTML structure instead of classes, which is a bit less flexible. You can also use the following to exclude the element you want to show from the elements that get hidden:
$('.person').not('#add_new_date'+id).hide()
You can use siblings to get the siblings of the clicked element.
$(document).on('click', '.person', function() {
$(this).show().siblings('.person').hide();
});
Get the siblings of each element in the set of matched elements, optionally filtered by a selector.
Docs: http://api.jquery.com/siblings
EDIT
function addMydate(userId) {
$('#add_new_date_' + userId).show().siblings('.person').hide();
}

Creating different functions on odd click and even click on link

I have 3 links which are the names of students.On clicking a link first time or odd number time,another div which contains the details of that student appears.On clicking the same link on second time or even number time, I need to hide the student div.Its working perfectly for me using data() event of jquery.
My requirement is if I click first student link,div with the details of first student will come.If I click the second student link,details of second student will come.If I again click on first link,the student div will hide since data() is associated with it(I think so). But I want to make it displayed because,previous click was on the second link.
My html is like this
George
Leo
Kelm
My jquery:
$("#sections ").on('click','.student_link',function(){
var clicks = $(this).data('clicks');
//alert(clicks);
if (clicks) {
$("#div_students").hide();
//alert(1);
}
else
{
$("#div_students").show();
//alert(2);
}
$(this).data("clicks", !clicks);
return false;
});
That is if I click George for first time, the corresponding div of student will appear,If I click Leo without clicking George to hide the div,I need div of Leo should come.If I click George again,I need div with details of George should come.But for me the div is hiding since the variable click is already true for George.I need the div to be displayed if previous click was not on the current link.
You can use the following code:
$("#sections").on('click', '.student_link', function () {
var prevClickedId = $('#sections').data('prevId');
if (prevClickedId != $(this).attr('id')) {
// This was not the previous clicked element
} else {
// This was the previous clicked element
}
$('#sections').data('prevId', $(this).attr('id'));
return false;
});
You can so something similar to this:
$("#sections").on('click', '.student_link', function(){
if( $("#div_students").data('viewing') == $(this).attr('id') ) {
$("#div_students").hide().data('viewing', '');
}
else {
$("#div_students").show().text($(this).attr('id'));
$("#div_students").data('viewing', $(this).attr('id'));
}
return false;
});
Here's a working example: https://jsfiddle.net/7qwsguoq/
Why don't just use jquery toggle() method? it hides element when it's visible, and shows when it's hidden.

I want to populate custom div based subcategory list as per selected main category list with Json callback

I have created a custom item list. It's not a regular html list element, it's custom div based item list with Jquery onclick event. This is my Jquery code:
$(document).ready(function(){
$(document).find("div[id^='cat']").live('click', function()
{
var currentId = $(this).attr('id');
$.getJSON('get_categories.php', {catName:$("#"+currentId).text()}, function(data)
{
$.each(data.CatList, function(i,itemList)
{
var tblRow =
"<div class='shop_options' id='cat" + itemList.id + "' onclick=''" +">"
+itemList.sub_cat+"</div>";
$(tblRow).appendTo("#subCatList");
});
});
});
});
I am able to append the sub category as per the main category item with onclick. But the problem is after clicking on one category whenever I click on any other category to populate the sub category data then it just amends the other category data as well as with previously selected main category item list.
For example if I select Fruits, then it displays the data Mango, Banana, Orange and again whenever I select any other category like Flowers then it displays Flowers's subcategory list Rose, Lotus, Marigold underneath Fruits subcategory list.
I want to clear the previously selected category items. How to do that, please give your experts advice. Many thanks in advance.
Use
$("#subCatList").empty();
before append.
use html() to make the div empty
$.getJSON('get_categories.php', {catName:$("#"+currentId).text()}, function(data)
{
$("#subCatList").html() //here add this
$.each(data.CatList, function(i,itemList)
{
var tblRow =
"<div class='shop_options' id='cat" + itemList.id + "' onclick=''" +">"
+itemList.sub_cat+"</div>";
$(tblRow).appendTo("#subCatList");
});
});

jquery+php send values

i have forms createds that require a value , as this this forms edits different users.
so how would i send that value from jquery? i know how to do it with combobox , but i want to do it from links :
like - name [details] when someone clicks on details the forms will pop up, so i wana mimic index.php?id=2 but with jquery, anyideas?
Do you want to display some content via JQuery with a link? Modify the selector to point to the correct DOM object, eg. an anchor tag with class "details"
$('a .details').click(function ()
{
$.get(
'index.php?id=2',
function(html)
{
$('#results').html(html);
});
});
if you want to load the content the link is pointing to, use (untested)
$('a .details').click(function ()
{
var anchor = this;
$.get(
$(anchor).attr('href'),
function(html)
{
$('#results').html(html);
});
});
if the id is stored in the li element, you can get the "id" attribute by using:
$('li').attr('id');
$('a .details').click(function ()
{ $.get( 'index.php?id=2', <--------------I need to pass that id=2 from a link thats created dynamicaly.
function(html)
{ $('#results').html(html); });});
for example, using
a
<ul id="cat" >
</ul>
i can acces , "cat" through jquery, loop through the cat elements and each li id=
will be clickable with some css
I cant figure out how to do it with normal text links!
thanks anyway dspinozzi

Categories