0

Script:

var degress = GiveDegrees();

$(function () {
    //alert(GiveDegrees());  //output: 89,91,87,90,91
});

function GiveDegrees() {
    var divs = $('p.wx-temp');
    var degrees = new Array();

    $.each(divs, function (i, j) {
        degrees.push(stringToNum($(this).text()));
    });

    return degrees;
}

function stringToNum(str) {
    return str.match(/\d+/g);
}

I have this :

$("p.wx-temp").each(degress, function (index) {
    $(this).append(degress[index], "<sup>°C</sup>");

    alert("I works");
});

But It does not work. when I write like this:

$("p.wx-temp").each(function (index) {
    $(this).append("<sup>°C</sup>");

    alert("I works");
});

It works. Is there no overload method for .each() function .each(data, function (index)) ?

degrees array is integer array that include five int as 89,91,87,90,91. How can I use .each function to add array values to each p element.

Thanks.

2
  • {89,91,87,90,91} is not a valid object. Commented Sep 6, 2012 at 8:19
  • its array of integer. It is only displaying not real format. Commented Sep 6, 2012 at 8:21

2 Answers 2

3

Why would you need to bring degress into the each at all? It’s already available, using closures:

$("p.wx-temp").each(function(index) {
    $(this).append(degrees[index], "<sup>°C</sup>");
    alert("I works");
});

OR

var $elems = $("p.wx-temp");
$.each(degrees, function(i, val) {
    $elems.eq(i).append(val, '<sup>°C</sup>');
});

If the elements length matches the array length, it should make no difference wich one you loop, but the first one is probably faster (although using a native array loop would be fastest).

Note that you are spelling degrees and degress differently in the two sections.

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

1 Comment

This is solution.Thanks It still does not work for me :(( I thinks there is another problem. When I write with alert degress[0] out of each function it works.
2
var degrees = [89,91,87,90,91];
$("p.wx-temp").each( function (index) {
    $(this).append(degrees[index], "<sup>°C</sup>");

    alert("I work");
});

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.