Avatar of FairyBusiness
FairyBusiness
Flag for United States of America asked on

How to capture which CLASS is clicked in jquery?

Hi, for my nav menu I want all of the regular men links (not any from the drop down menus) to default to their first child link.  So when you click on 'about us' it goes to 'kitchen master'

but because its classes its needs not to default to the first one everytime but to which class was clicked so that it knows to go to the next link after that one, not 'kitchen master' everytime.

$('.parent').click(function () {
    var li =  $('li').next();
    var target_url = $('.ul_1 > ' + li + ':eq(0) > a').attr('href');
	alert(target_url);
    if(target_url!=undefined) window.location.href=target_url;
    return false;
});

Open in new window


Any ideas?

http://gowiththemaster.com/tleithoff/index.php
JavaScript

Avatar of undefined
Last Comment
FairyBusiness

8/22/2022 - Mon
haloexpertsexchange

HainKurt

on page load, check the url with li values, if they match change the class

or just this code should work I hope :)

$('.ul_1 li[href=' + window.window.location.href + ']').attr('class');
HainKurt

above code removes the class, to add a selectedClass:

$('.ul_1 li[href=' + window.window.location.href + ']').attr('class','selClass');

or

$('.ul_1 li[href=' + window.window.location.href + ']').addClass('selClass');
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
FairyBusiness

ASKER
I'm not sure where to put that in my function. . .  or what I would do with it.  ???
HainKurt

if this does not make sense, meaning, I could not get what you mean...

for example "regular men links"
FairyBusiness

ASKER
they dont have classes on them currently. do I need to add classes to them??  they are all in one class the ul class
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
HainKurt

do you have a link so we can see what is wrong
FairyBusiness

ASKER
HainKurt

look at this sample...

when this page is called with id=2 in url, for example: http://some.domain.com/somedir/index.php?a=1&id=2&b=XYZ

it will find the param value of id, which is 2, and then find the a in UL with href like "id=2", and find parent of that a, and add class "selected"...

hope it helps...
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<style>
.selected {color:red; background-color:yellow;}
</style>
	
<script type="text/javascript">
function getParameterByName(name) {
  var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
  return match && decodeURIComponent(match[1].replace(/\+/g, ' '));  
} 
	
$(function(){
  var id=getParameterByName(id);
  // to test this, uncomment below, or save as index.htm, open it browser, then add ?id=2 to url and refresh the page, you will see class will change, then play with id=1, id=2, id=3, id=4
  // id=2;
  var a = $('#menu a[href*="id='+id+'"]')[0];
  $(a).parent().addClass("selected"); 
});
</script>

<ul id=menu>
	<li><a href="index.php?id=1">A</a>
	<li><a href="index.php?id=2">B</a>
	<li><a href="index.php?id=3">C</a>
<ul>

Open in new window

Your help has saved me hundreds of hours of internet surfing.
fblack61
FairyBusiness

ASKER
that link didnt work for me
HainKurt

or a better example, modified version of above... save as some.html and open and click links...
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<style>
.selected {color:red; background-color:yellow;}
</style>
	
<script type="text/javascript">
function getParameterByName(name) {
	var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
	return match && decodeURIComponent(match[1].replace(/\+/g, ' '));  
} 
	
$(function(){
	var id=getParameterByName("id");
	var a = $('#menu a[href*="id='+id+'"]')[0];
  $(a).parent().addClass("selected"); 
});
</script>

<ul id=menu>
	<li><a href="?id=1">A</a>
	<li><a href="?id=2">B</a>
	<li><a href="?id=3">C</a>
<ul>

Open in new window

HainKurt

huh! "that link didnt work for me"

that is just a sample :) not a real link...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
FairyBusiness

ASKER
oh  lol  ok  hold on let me check out this code :P
FairyBusiness

ASKER
ok I like this script, but i'm still not sure how to make one of my menu links default to its first li child link

i dont know how it would get the id of the first child link bc it wont be click for this.  if you click 'about us' i want it to go to 'kitchen master'

http://gowiththemaster.com/tleithoff/index.php

does that make sense?
HainKurt

who/how is created this link?

<A class=parent href="index.php?id=1&amp;type=1blurb&amp;table=menus" jQuery1305744323744="1">about us</A>

can you change "table=menus" to "table=submenus"
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
FairyBusiness

ASKER
it comes from a menus table in my database, and the links in the drop down menu come from a table called submenus.  no I can simply change that, its that way bc of where it comes from.  I'm just trying to find a way for it change hrefs

if I did this:

$('.parent').click(function () {
        //var parent_element=$(this).parent();
        var target_url=$(' .ul_1 > li:eq(0) > a').attr('href');
        alert(target_url);
        if(target_url!=undefined) window.location.href=target_url;
        return false;
});

Open in new window


it works for the first link, but it does not work for the rest.  they go back to 'about us' first child link 'kitchen master' so i need it to some how know which .parent was clicked and then to go to the next li
HainKurt

if you comment out this function all together what happens?
FairyBusiness

ASKER
ok I commented it out.  but you will see error messages bc its trying to access the database but beyond ids, and titles, there is not much info there, so it doesn't have stuff to retrieve
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
HainKurt

still confused... could not get the issue, could not get what are you trying to do :)

so, in my words: do you want to go to "Kithcen Master" (the first child link) when you click "About Us"
same for "Kraftmaid", when clicked here, do you want to go to "Inspiration" page?
FairyBusiness

ASKER
yes!!!

when clicked:

'about us' -> 'kitchen master'
'kraftmaid' -> 'inspiration'

right now all of the main links just to back to kitchen maid

I uncommented it now:

$('.parent').click(function () {
        //var parent_element=$(this).parent();
        var target_url=$(' .ul_1 > li:eq(0) > a').attr('href');
        alert(target_url);
        if(target_url!=undefined) window.location.href=target_url;
        return false;
});
FairyBusiness

ASKER
kitchen master not kitchen maid
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
HainKurt

got the issue :) lets solve this by trying this:

$('.parent').click(function () {
        var target_url=$('ul li a')[0].attr('href');
        alert(target_url);
        if(target_url!=undefined) window.location.href=target_url;
        return false;
});
HainKurt

i forgot to add "this"

$('.parent').click(function () {
        var target_url=$('ul li a', this)[0].attr('href');
        alert(target_url);
        if(target_url!=undefined) window.location.href=target_url;
        return false;
});

or

$('.parent').click(function () {
        var target_url=$('ul li a', $(this))[0].attr('href');
        alert(target_url);
        if(target_url!=undefined) window.location.href=target_url;
        return false;
});
 
FairyBusiness

ASKER
nope, neither of those are working right
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
HainKurt

or this

$('.parent').click(function () {
  var a = $('ul li a:first', this);
  if (a) window.location.href=a.href;
  return false;
});

or

$('.parent').click(function () {
  var a = $('ul li a:first', $(this));
  if (a) window.location.href=a.href;
  return false;
});
FairyBusiness

ASKER
no, those work worse lol
HainKurt

ok, try this...
<script type="text/javascript">
$(function(){
	$('.parent').click(function() {//alert(this.href);
	  var a = $('ul li:first a', $(this).parent());
	  if ($(a)){
	    alert($(a).attr("href"));
	    window.location.href=$(a).attr("href");
	  };
	  return false;
	}); 
});
</script>

Open in new window

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
FairyBusiness

ASKER
interestingly enough it only works for kraftmaid!
ASKER CERTIFIED SOLUTION
HainKurt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
HainKurt

it should work! uncomment the alert and see if it is giving some message....

can you please update the site so i can check too :)
HainKurt

and i see it is working :) clear the cache since you put that script into js file... or close and start browser before testing...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
HainKurt

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
HainKurt

finally this can be closed I guess :)
FairyBusiness

ASKER
its working!!!!! yeah!!!!!!!!!!!!!!!!  thank you so much :))