Pass id of anchor to use in function: JQUERY

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: May 2009
Posts: 20
Reputation: cjay175 is an unknown quantity at this point 
Solved Threads: 0
cjay175 cjay175 is offline Offline
Newbie Poster

Pass id of anchor to use in function: JQUERY

 
0
  #1
Jun 24th, 2009
Hey,

I was wondering if someone could help me with this.

I want to, when an anchor link is clicked, the id of the link is grabbed by jquery then passed to a function. The id is would be the same as the id of the div that is used for the function (highlight).

If someone could tell me or point me in the right direction.

Here is what I have right now.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2.  
  3. $(function(){
  4. $('#mainq a').click(function(){
  5. var id = this.id();
  6. $('this.id').highlightFade({speed:2000});
  7. });
  8. });
  9.  
  10. </script>

The HTML is like this (its for FAQ section):

QUESTIONS SECTION:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="mainq" style="padding:10px;" class="mainq"><a name="top"></a>
  2.  
  3. <h3>Payment and Billing.</h3><br />
  4. <a href="#aq1" id="qa1" onclick="javascript: getID(this)">What is water?</a><br />
  5. <a href="#aq2" id="qa2" onclick="javascript: getID(this)">What is copper?</a><br />
  6. </div>
  7.  
  8. <h3>Payment and Billing.</h3><br />
  9. <a href="#aq1" id="qa1" onclick="javascript: getID(this)">What is water?</a><br />
  10. <a href="#aq2" id="qa2" onclick="javascript: getID(this)">What is copper?</a><br />
  11. </div>


ANSWERS HERE:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="maina">
  2. <hr /><div id="qa1" class="answertext"><a name="aq1"></a><h3>What is Water?</h3>
  3. Water is H2O.<br /><a href="#top"><img src="images/top_arrow.jpg" border="0"/></a></div>
  4.  
  5. <hr /><div id="qa2" class="answertext"><a name="aq2"></a><h3>What is Copper?</h3>
  6. Copper is cu.<br /><a href="#top"><img src="images/top_arrow.jpg" border="0"/></a></div>
  7. </div>


The code is a little messy but I am just testing right now.

So if someone clicks on #aq1 link the id "aq1" is grabbed and used for the highlightFade function. It would highlight the div with id "aq1".

Thanks so much for any help
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 439
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Pass id of anchor to use in function: JQUERY

 
0
  #2
Jun 24th, 2009
1. Don't use ID like you do. ID's are meant to be unique but your first section use "qa1" and "qa2" twice in both anchors and is then used in the divs as part of the second section.

2. You dont need the "OnClick" attribute if using jQuery (it is not recommended practice)

3. The easiest way would be to change your anchors id to different attributes (perhaps your own) or seeing as you are using the id in the href attribute use that instead e.g.
  1. <div id="mainq" style="padding:10px;" class="mainq"><a name="top"></a>
  2. <h3>Payment and Billing.</h3><br />
  3. <a href="#aq1">What is water?</a><br />
  4. <a href="#aq2">What is copper?</a><br />
  5. <h3>Payment and Billing.</h3><br />
  6. <a href="#aq1">What is water?</a><br />
  7. <a href="#aq2">What is copper?</a><br />
  8. </div>
And then change your JavaScript to read...
  1. <script type="text/javascript">
  2. $(function() {
  3. $('#mainq a').click(function() {
  4. var id = $(this).attr('href');
  5. $(id).highlightFade({speed:2000});
  6. });
  7. });
  8. </script>

With the above code you then seperate the actual javascript from the page view. I have changed your id variable to get the href attribute which would be '#qa1' or '#qa2' and then it finds this element using another jQuery selector $(id).

Hope this helps
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 20
Reputation: cjay175 is an unknown quantity at this point 
Solved Threads: 0
cjay175 cjay175 is offline Offline
Newbie Poster

Re: Pass id of anchor to use in function: JQUERY

 
0
  #3
Jun 25th, 2009
Hey Fungus,

Sorry for the late reply I was out in the mountains yesterday.

Thanks a lot for the help it is working perfect.

Like I mentioned the code is messy just because I kept adding more and more stuff to it trying to get it to work. I am just starting to learn javascript and jquery.

thanks again for the help.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 439
Reputation: Fungus1487 is on a distinguished road 
Solved Threads: 50
Fungus1487's Avatar
Fungus1487 Fungus1487 is offline Offline
Posting Pro in Training

Re: Pass id of anchor to use in function: JQUERY

 
0
  #4
Jun 26th, 2009
No problem, if your all sorted you can mark the thread as solved.
When Autumn Falls [ http://www.whenautumnfalls.co.uk ] &&
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 1697 | Replies: 3
Thread Tools Search this Thread



Tag cloud for JavaScript / DHTML / AJAX
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC