0

Currently I have this hidden input in a loop and it works:

<input type="hidden" class="md" name="business-222" value="11|55|some name| some address|800-444-8800|somepage.html" />

<input type="hidden" class="results" name="business-222" value="22|77|other name| other address|800-444-8800|otherpage.html" />


$(".results").each(function(){
  var text = $(this).attr('value').split('|');
  var name = text[2];
  ...
});

I was womndering what would be a good way to get the data from this structure below without using hidden elements..

<tr>
  <td><a href="/somepage.html">some name</a></td>
  <td>some address</td>
  <td>some phone</td>
</tr>

<tr>
  <td><a href="/otherpage.html">other name</a></td>
  <td>other address</td>
  <td>other phone</td>
</tr>

I was thinking to make it and somehow select first second third td ?

Thanks !!

2
  • it depends what you want out of that structure. Is it the link, or the contents of the anchor tag? Commented Jan 17, 2012 at 8:17
  • Aram, I kind of need all whats there its for google map with multiple markers, external javascript with .each keeping html and js very minimalistic... Commented Jan 17, 2012 at 8:20

4 Answers 4

1

I was womndering what would be a good way to get the data from this structure below without using hidden elements..

Try:

$("#tableID > tbody > tr").each(function(){
  var first = $("td:eq(0)", $(this)).html();
  var second = $("td:eq(1)", $(this)).html();
  var third = $("td:eq(2)", $(this)).html();

  alert(first);
  alert(second);
  alert(third);

});

Working Example

Sign up to request clarification or add additional context in comments.

1 Comment

Would this work for multiple rows? The variables will get written over, right?
0

I think you need something like this:

$('table tr').each(function(index, elem) {
    $(this).find('td').each(function(index, elem) {
        var obj = {};
        switch(index) {
        case 0:
            obj.name = $(this).find('a').text();
            break;
        case 1:
            obj.address = $(this).text();
            break;
        case 2:
            obj.phone = $(this).text();
            break;
        }

        // do something here with the obj, for example put it in a global array or so
    });
});

Comments

0

Why not just output the data you need as a JS-object? For example:

    data = {};
    data.poi1 = {
        val1    : 11,
        val2    : 55,
        name    : 'some name',
        address : 'some address',
        phone   : '800-444-8800',
        page    : 'somepage.html'
    };
    data.poi2 = {
        val1    : 22,
        val2    : 77,
        name    : 'other name',
        address : 'other address',
        phone   : '800-444-8800',
        page    : 'otherpage.html'
    };
    alert(data.poi1.val1);

Comments

0
Thanks for all the answers Its working now just have a minimal hidden for lat/long will clean up that later..

<tr class="results">
<td><div class="m maps" id="ms_1"></div><input type="hidden" class="geo" name="business-12345" value="32.9131|-96.8822" /></td>
<td><a href="/architects">Architects</a></td>
<td>123456 Abc Ln, Dallas, TX 75056, USA</td>
<td>888-222-0099</td>
</tr>


$(".results").each(function() {
    var coords = $("td:eq(0)", $(this)).find('.geo').attr('value').split('|');
    var name = $("td:eq(1)", $(this)).html();
    var address = $("td:eq(2)", $(this)).html();
    var phone = $("td:eq(3)", $(this)).html();
    var latlng = new google.maps.LatLng(coords[0], coords[1]);
    var marker = createMarker(latlng, '<span class="fs10"><strong>'+name+'</strong><br />'+address+'<br />'+phone+'</span>');
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.