473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to get related data of each link from database onclick of links?

36
I have list of Areas .
Expand|Select|Wrap|Line Numbers
  1. <ul>            
  2.     <li><a class="my-button" id="a1" href="#">Area1</a></li>
  3.     <li><a class="my-button" id="a2" href="#">Area2</a></li>
  4.     <li><a class="my-button" id="a3" href="#">Area3</a></li>
  5.     <li><a class="my-button" id="a4" href="#">Area4</a></li>
  6.     <li><a class="my-button" id="a5" href="#">Area5</a></li>
  7. </ul>    
  8.  
When i click on any area it should get items related to that specific area .

I have a javascript for that .

Expand|Select|Wrap|Line Numbers
  1. <script>
  2. $(document).ready(function() {
  3.      $('body').on('click', '#a1', function() {
  4.  
  5.     $("#display").load("area.php?areaid=1");
  6.  
  7.   });
  8. });
  9. </script>
  10. <script>
  11. $(document).ready(function() {
  12.      $('body').on('click', '#a2', function() {
  13.  
  14.     $("#display").load("area.php?areaid=2");
  15.  
  16.   });
  17. });
  18. </script>
  19. <script>
  20. $(document).ready(function() {
  21.      $('body').on('click', '#a3', function() {
  22.  
  23.     $("#display").load("area.php?areaid=3");
  24.  
  25.   });
  26. });
  27. </script>
  28. <script>
  29. $(document).ready(function() {
  30.      $('body').on('click', '#a4', function() {
  31.  
  32.     $("#display").load("area.php?areaid=4");
  33.  
  34.   });
  35. });
  36. </script>
  37. <script>
  38. $(document).ready(function() {
  39.      $('body').on('click', '#a5', function() {
  40.  
  41.     $("#display").load("area.php?areaid=5");
  42.  
  43.   });
  44. });
  45. </script>
  46.  
The above code was suggested by a bytes member Anas Mosaad and this is working fine .

What i want to ask is that , are there any other better methods to do this ?

Because you can see the javascript above , which has repeated as many times as number of links are
displayed . There might be more number of links in my site and on different pages . So copying same code everywhere isn't worth right ?

So was looking for loop type of script such that any link is clicked , it gets data related to that .
And also i need a transition on changing data in that div tag .

Suggestions are appreciated . Thank you .
Feb 1 '13 #1
8 5835
Rabbit
12,516 Expert Mod 8TB
Instead of hard coding the areaid, use the second character of the anchor element's id.
Feb 1 '13 #2
yateesh
36
How do i do that ? And does that make script small or same as i displayed ?
Feb 1 '13 #3
Rabbit
12,516 Expert Mod 8TB
You do it by passing the element to the function. Then you can get the id property of the element that called the function.

It makes it smaller, you only need one function.
Feb 1 '13 #4
yateesh
36
Okay . Can you display that piece of code as i do not have much knowledge on this . Learning it . Or tell me where do i find a solution to that .
Feb 1 '13 #5
Rabbit
12,516 Expert Mod 8TB
You can pass an element like so:
Expand|Select|Wrap|Line Numbers
  1. <div id="somevalue" someevent="somefunction(this)">
In the function, you can retrieve the id like this:
Expand|Select|Wrap|Line Numbers
  1. function somefunction(ele) {
  2.    alert(ele.id);
  3. }
If you're having trouble with basic javascript such as this, you may want to invest the time in a tutorial before trying to take on such a large project. This and many of your other questions are covered in a basic tutorial of javascript.
Feb 1 '13 #6
yateesh
36
Thank you . I thought of learning while doing itself . But that takes a lot time to complete as you said .
Feb 1 '13 #7
Rabbit
12,516 Expert Mod 8TB
What takes a lot of time to complete? I didn't say anything took a lot of time to complete.

Learning by doing is good if you already know the basics. But if you don't know anything about the subject, it wastes a lot more time because you end up having to rewrite the same code over and over.

Much like you're doing here. Had you learned the basics in the first place, you wouldn't have spent all that time writing the same code 5 times only to learn the correct way, scrap the original code, and rewrite it.
Feb 1 '13 #8
yateesh
36
:-) With knowing some basics i started this . But by starting this one , i have come to know may things which i couldn't get without putting hands on this .

Somethings are achieved by trial and error right ?
Feb 1 '13 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Matt | last post by:
I created 3 hyperlinks, when the user click each link, it will change the color of the text of a link. For example, when user clicks Link1, text Link1 will become red color, but Link2 and Link3...
0
by: Scanner2001 | last post by:
I am researching some options for loading hierarchically related data from xml into an sql server database. I do not have the primary keys in the source xml files. The primary keys are identity...
2
by: Jim in Arizona | last post by:
Usually, If i need special formatting, I don't use the datagrid control and use a loop that processes a table for each record read from the database (as in classic asp) like so: ...
2
by: Jeremy Dillinger | last post by:
I have a program setup to pull data from a database. My database table has things such as (category, Item, price, etc.) In my program I want to have multiple list boxes that will have a pull down...
2
by: Bob | last post by:
I got three related datagrid views one parent and two children of the same. The two child tables contain many thousands of records and some of the contents are bitmap files in a sql server...
3
by: Aaron | last post by:
Hi, It is possible to establish relationships between tables within a dataset and this allows some useful features. For example, given a row from a table you can use GetParentRow,...
3
by: LynneH | last post by:
I have data on a shared drive that needs to be transferred to a local drive. there are about ten databases that need this. I then have a central database that links all these databases together. I...
0
by: akshalika | last post by:
I am new to Biztalk. In my project we need to connect oracle database and insert data into oracle table using BizTalk project. I use WCF Adapter pack(SP2). I create biztalk project then using Consume...
2
mreed72
by: mreed72 | last post by:
I have a problem and can't get clear enough to figure it out. 2 tables in my database are related to each other. table1 = contacts table2 = Jobs both are a 1to1 relation, using the standard...
3
by: BarbQb | last post by:
Hi All, I currently have a form with a multiselect list box that shows Vehicle IDs (Column 0) and Vehicle Statuses (Column 1) by customer. The VehicleID column is the bound column. Occasionally...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.