3

I have a table with a couple of rows with two cells in each row and each cell has couple of info like office name, address and phone number etc. Using jquery for each, the address is being pulled out from each cell and fed into google map geocoder object to get the point and plot it in the map. Now at each hit of an Address value, I would also like to grab the unique value of phone and office name from the current cell from which jquery is getting address value..I need those values so i can display those values in the InfoWindow of the map? How do I get those values?

<table class="OfficeInfo" border="0" style="width: 100%" cellspacing="10px" cellpadding="15px">
  <tr>
    <td class="Office1" style="width=40%">  
     <span class="OfficeName">
     <div id="ctl00_PlaceHolderMain_ctl00_ctl17_label" style='display:none'>Office1Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl17__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl17_label"><div class="ms-rtestate-field"><a href="/" target="_blank">St. Francis Hospital</a></div></div>
     </span>
     <span class="Address">
     2001 86th Street West  <br />Indianapolis, IN 46260        
     </span> <br />
     <span class="Phone">
     (402) 123-1234</span><br /><br />
     <a class="mapdirectionsLink" href="#">map &#38; directions&#62;</a><br /><br />
     <span class="Hours">
     MTW:9:00AM-5:00PM</span>
    </td>

    <td class="Office2" style="width:40%">  
     <span class="OfficeName">
     <div id="ctl00_PlaceHolderMain_ctl00_ctl21_label" style='display:none'>Office2Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl21__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl21_label"><div class="ms-rtestate-field"><a href="/" target="_blank">St. Margaret's Hospital</a></div></div>
     </span>    
     <span class="Address">
     8075 North Shadeland Avenue <br />Indianapolis, IN 46250</span><br />
     <span class="Phone">
     (316) 123-3245</span><br /><br />
     <a class="mapdirectionsLink" href="#">map &#38; directions&#62;</a><br /><br />
     <span class="Hours">
     MTW:9:00AM-5:00PM</span>
    </td>
  </tr> 

   <tr>                                   
    <td class="Office3" style="border-top:1px dotted silver;  width:40%;">
     <span class="OfficeName">
     <div id="ctl00_PlaceHolderMain_ctl00_ctl25_label" style='display:none'>Office3Link</div><div id="ctl00_PlaceHolderMain_ctl00_ctl25__ControlWrapper_RichLinkField" class="ms-rtestate-field" style="display:inline" aria-labelledby="ctl00_PlaceHolderMain_ctl00_ctl25_label"><div class="ms-rtestate-field"><a href="/" target="_blank">Munster Women's Center</a></div></div>
     </span>
     <span class="Address">
     395 Westfield Road <br />Noblesville, IN 46060</span><br />
     <span class="Phone">
     (316) 123-3245</span><br /><br />  
     <a class="mapdirectionsLink" href="#">map &#38; directions&#62;</a><br /><br />
     <span class="Hours">
     MTW:9:00AM-5:00PM</span>
    </td>
    <td  style="border-top:1px dotted silver;  width:40%">                          
    </td>
  </tr>               

 </table>


          function codeAddress() {
    var infowindow = new google.maps.InfoWindow({}); 
    $('span.Address').each(function(index) {
        var addy = $(this).text();
        var off_name = $(this).siblings('.OfficeName').children(.ms-rtestate-field).text();
        var infowindow = new google.maps.InfoWindow({  content: 'Hello world'   });

        geocoder.geocode( { 'address': addy}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                map: map, 
                position: results[0].geometry.location,
                title:addy          
            });


            //var currentCell=(this).closest(td).html()); 
            // Adding a click event to the marker 
            google.maps.event.addListener(marker, 'click', function() { 
                infowindow.setContent('<span style="color:#808080; font-size:13px; font-family:Trebuchet">'+addy 
                           +'<br> <a href="http://maps.google.com/maps?saddr=&daddr=' + this.position.toUrlValue() + '" target ="_blank">Get Direction To Here<\/a>'+                      
                           off_name + '</span>'); 
                infowindow.open(map, this); 
            });  


            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    });
}
Jack
  • 9,156
  • 4
  • 50
  • 75
AJSwift
  • 709
  • 4
  • 12
  • 26
  • Might be smart to also add your javascript, or better: just paste the relevant parts, and add the rest to a jsfiddle (http://jsfiddle.net) – Arend Nov 17 '11 at 22:03
  • I have now added the javascript too... I want to get the office name and phone number on the current context of 'this'.. – AJSwift Nov 17 '11 at 22:06
  • Not that I want to nitpick on details, I recommend you identify your elements with a unique id, instead of classes. This way you could target specific elements. Although you can do this with classes, a class is intended to be a generic declaration that can be shared by many elements whereas an id is specific and unique to a single element. – Stephane Gosselin Nov 17 '11 at 22:09
  • @stefgosselin - by the looks of the mangled ctl00_... id on the span, he is using ASP.NET. Since webforms mung up the ID's so badly it is often easier to use class names (that is what I do) rather than sprinkle <% [ControlID].ClientID %> code all over the place. However, I agree that if you want unique elements you need at least unique class names, if you are not using the ID. – Tim Hobbs Nov 17 '11 at 22:18

6 Answers6

7

Depending on the current context of this, then this should work:

$(this).parent().find('.officeName').html();
Yes Barry
  • 9,514
  • 5
  • 50
  • 69
  • 1
    This did it.. THannks to everyone who took their efforts to answer my question and solve my problems. I really appreciate it! AJ – AJSwift Nov 18 '11 at 00:07
  • I'm using dynamic html generated from server-side script & this method not working for me. Am I missing somewhere? Here is my my script example:- `$(this).parent().find('#msg').html();` – Wafie Ali Mar 30 '17 at 07:10
  • @WafieAli I am not sure. I would need to see more of your script to determine that. Can you make a paste of it in pastebin or something? – Yes Barry Mar 30 '17 at 14:57
  • @YesBarry this is example for my attempts:- [html](https://pastebin.com/RnfJC10m), [script](https://pastebin.com/x70QhRhM) the expected results from script is that, the child table will fetch value from one of the column of it's parent cell. – Wafie Ali Mar 31 '17 at 01:42
  • 1
    @Yes Barry, this did it thank you. $(this).parent().parent().find('a span.family-price').attr('style', 'visibility: hidden;'); – Olivier Feb 15 '19 at 23:36
  • 1
    @Olivier You can shorten that using `.parents(selector)` or `.closest(selector)` depending. If you want to go two parents up, you could do `$(this).parents().eq(1).find('a span.family-price')...`. – Yes Barry Feb 16 '19 at 02:08
2

We can do this by using jquery OR Javascript. here we are going to discuss with email id updates.

in bellow example a pop up window will open with auto fill email id from parent window. after update, a email will automatically update in parent window text box and pop up window will have closed autocratically.

Example:

1) Create file index.html as a parent windows

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<title></title>
<head></head>

<body>
<table>

<tr>
<td colspan=”2″>Example for update email id.</td>
</tr>
<tr>
<td>Email Id:</td>
<td>
<input type=’text’ name=”emailID” id=”emailId” value=”demo@demo.com”></td>
</tr>
<tr>
<td>
<input type=”button” name=”update” value=”Update”
onClick=’window.open(“update_popup.html”, “”, “width=400, height=300″)’>
</td>
</tr>
</table>
</body>
</html> 

2) Create file update_popup.html as a pop up window where email id auto fill from parent window for update.

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
<title></title>
<head></head>
<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
<script>
$(document).ready(function(){

//== pre fill parent window email id in popup window for update

var emailId = window.opener.document.getElementById(“emailId”).value;
$(“#emailId”).val(emailId);

//=== update updated email id in to parent window

$(“#Save”).on(‘click’,function(){

var updated_emailId = $(“#emailId”).val();
window.opener.document.getElementById(“emailId”).value = updated_emailId;
window.close();
});
});
</script>

<body>
<table>
<tr>
<td>Email Id:</td>
<td>
<input type=’text’ name=”emailID” id=”emailId” value=””></td>
</tr>
<tr>
<td><input type=”button” name=”Save” id=”Save” value=”Save”></td>
</tr>
</table>
</body>
</html>

for more details..

http://www.delhincrjob.com/blog/how-to-get-the-parent-window-element-value-in-popup-window-using-jquery/

2

$(this).siblings('.Phone').html() and $(this).siblings('.Address').html()

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
0

$(this).parent().find(".OfficeName").text() gets the office name and $(this).parent().find(".Phone").text() gets the phone for each address.

jsfiddle - click on an address and it'll show the name and phone in an alert.

Tim Hobbs
  • 2,017
  • 17
  • 24
0
$('td.Office1').chilldren('.Phone')

Will get you the phone number for Office 1 from the Office 1 td

$('span.Address').siblings('.Phone')

Will get you the phone number from the Address spans

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
0

Here is a small and simple but good for starters, a PDF for Jquery. Try using Jquery Cheat Sheet as it contains solution to most of your problems.