开发者

getElementById('pcode') many elements all numbered

开发者 https://www.devze.com 2023-02-02 03:19 出处:网络
I have a a document that when an link is clicked a table of results is displayed within each row of this table there is a TD element with a SPAN tag, inside of this is an id which starts at 2 and can

I have a a document that when an link is clicked a table of results is displayed within each row of this table there is a TD element with a SPAN tag, inside of this is an id which starts at 2 and can increment up to 400 (i.e. id="pcode2").

I have a need to get the inner HTML values of this and write them into a Javascript function?, array? string? followed by a comma to use on another Javascript function for a map.

The data is pre sorted and the map is called by clicki开发者_StackOverflowng a link to get the route. the span id's will never reach more than 400.

the end destination needs to look like this..

var via = ("pcode2," "pcode3,", "pcode4");

Where pcode is equal to the innerHTML value of the SPAN which is a postcode.

Thanks

Justin


You mean something like:

var values = [];

for(var i = 2; i <= 400; i++) {
    var element = document.getElementById('pcode' + i);
    if(element === null) {
        break;
    }
    values.push(element.innerHTML); // or element.innerHTML + ','
}

?

If not, you should clarify our question.

Not sure why you need the comma though. If you want to concatenate the values later, you can just use join():

var str = values.join(',');

Example:

var values = ['a','b','c'];
var str = values.join(',');
// gives "a,b,c"

Update: From the documentation, I think you have to do this (assuming map is globally available):

function GetRoute() { 
    var from = document.getElementById('inpAddr').value; 
    var locations = [from]; 
    for(var i = 2; i <= 400; i++) { 
        var element = document.getElementById('pcode' + i); 
        if(element === null) { break; } 
        locations.push(element.innerHTML); 
    } 
    var options = new VERouteOptions(); 
    options.DrawRoute = true; 
    map.GetDirections(locations,options); 
}

Note that locations is just a single array. In your original code, you create a nested array. And you don't seem to need a trailing comma, just an array of locations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号