SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict kivison's Avatar
    Join Date
    Dec 2004
    Location
    Whitley Bay, Tyne & Wear, UK
    Posts
    246
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Multiple lines in table reusing same jquery code

    Hi Gurus

    I need a bit of help on a jquery submission

    I ahve a php script which iterates through a db call. This produces a table of information like

    Name - Age - etc and a button saying add to list.

    When the button is clicked the relevant line is added to the list interactively via jquery using the placement.php page in the code below. Here is the code I use

    Code:
    $(document).ready(function() {
    
    		
    		$("#find_host").click(function(){
    		
    		var postcode = $('#place_postcode').val();
    		var max_distance = $('#max_distance').val();
    		var dataString = "postcode="+postcode+"&max_distance="+max_distance;
    		if(postcode.length>0){
    		  $.ajax({
    			type: "GET",
    			url: "includes/placement.php",
    			data: dataString,
    			success: function(server_response){
    			  $('#txtResult').html(server_response).show();
    			}
    		  });
    		}return false;
    		});
    Is fine for a single row in a table where there is only one #name and 1 #age but I want to reuse the code for every iteration of the line in the table.

    for instance
    Name-1 - Age-1 - etc and a button saying add to list. button_1
    Name-2 - Age-2 - etc and a button saying add to list. button_2
    Name-3 - Age-3 - etc and a button saying add to list. button_3
    Name-4 - Age-4 - etc and a button saying add to list. button_4

    So the jquery needs to be told which button to listen for i.e #find_host1 or #find_host2 I cant think of a way of doing this without also iterating the jquery for each button

    Code:
    $(document).ready(function() {
    
    		
    		$("#find_host1").click(function(){
    		
    etc etc
    
    $(document).ready(function() {
    
    		
    		$("#find_host2").click(function(){
    		
    etc etc
    Any help would be great.

    Thanks in advance

    Keith

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,527
    Mentioned
    84 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by kivison View Post
    So the jquery needs to be told which button to listen for i.e #find_host1 or #find_host2 I cant think of a way of doing this without also iterating the jquery for each button
    Try using the Attribute Starts With Selector
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •