| | |
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:
Solved Threads: 0
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.
The HTML is like this (its for FAQ section):
QUESTIONS SECTION:
ANSWERS HERE:
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
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)
<script type="text/javascript"> $(function(){ $('#mainq a').click(function(){ var id = this.id(); $('this.id').highlightFade({speed:2000}); }); }); </script>
The HTML is like this (its for FAQ section):
QUESTIONS SECTION:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<div id="mainq" style="padding:10px;" class="mainq"><a name="top"></a> <h3>Payment and Billing.</h3><br /> <a href="#aq1" id="qa1" onclick="javascript: getID(this)">What is water?</a><br /> <a href="#aq2" id="qa2" onclick="javascript: getID(this)">What is copper?</a><br /> </div> <h3>Payment and Billing.</h3><br /> <a href="#aq1" id="qa1" onclick="javascript: getID(this)">What is water?</a><br /> <a href="#aq2" id="qa2" onclick="javascript: getID(this)">What is copper?</a><br /> </div>
ANSWERS HERE:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
<div id="maina"> <hr /><div id="qa1" class="answertext"><a name="aq1"></a><h3>What is Water?</h3> Water is H2O.<br /><a href="#top"><img src="images/top_arrow.jpg" border="0"/></a></div> <hr /><div id="qa2" class="answertext"><a name="aq2"></a><h3>What is Copper?</h3> Copper is cu.<br /><a href="#top"><img src="images/top_arrow.jpg" border="0"/></a></div> </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
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.
And then change your JavaScript to read...
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
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.
HTML Syntax (Toggle Plain Text)
<div id="mainq" style="padding:10px;" class="mainq"><a name="top"></a> <h3>Payment and Billing.</h3><br /> <a href="#aq1">What is water?</a><br /> <a href="#aq2">What is copper?</a><br /> <h3>Payment and Billing.</h3><br /> <a href="#aq1">What is water?</a><br /> <a href="#aq2">What is copper?</a><br /> </div>
javascript Syntax (Toggle Plain Text)
<script type="text/javascript"> $(function() { $('#mainq a').click(function() { var id = $(this).attr('href'); $(id).highlightFade({speed:2000}); }); }); </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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
•
•
Join Date: May 2009
Posts: 20
Reputation:
Solved Threads: 0
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.
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.
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
Designdotworks [ http://www.designdotworks.co.uk ] Web / Graphic / Software Design
![]() |
Similar Threads
- c language problm, how to pass pointer to a function (C)
- How do I pass String into a function in C++? (C++)
- Pass an arrayList to a function (VB.NET)
- pass an array from one function to another (C)
- How to pass reference of a 2-D matrix to a function in C (C)
- C++ pass 2d array into function (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: please help me i need to make my own CHATROOM
- Next Thread: how do i pass arrays in javascript to php using POST method
Views: 1697 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
acid2 ajax ajaxcode ajaxhelp animate array automatically autoplay beta boarder box bug button calendar captcha card cart codes column cookies createrange() css cursor date debugger decimal design developer dom download dropdown element enter error events firefox firehose flash focus form frameworks getselection google gwt html htmlform iframe image() index java javascript javascripts jawascriptruntimeerror jquery jsp listbox maps marquee masterpage menu microsoft mimic mp3 mp4 offline onmouseover parameters php player post problem programming progressbar prototype rating redirect regex safari scale scriptlets search select size sources sql starrating text textarea toggle twitter validation variables w3c web website window windowofwords windowsxp xml xspf





