Boomer
Boomer

Reputation: 57

Put a variable in a link

I have below code for parse some json and make other things :

<!doctype html>
<html>
<head>
    <title>Parsing</title>
    <script type="text/javascript" src="jquery-2.1.0.min.js"></script>
    <link rel="stylesheet" href="css/index.css" type="text/css" />
</head>
<script>

    $.getJSON(
        'http://sath3g.altervista.org/index.php', { get_param: 'value' }, 
        function(data) {
        // 'data' is an array of objects here
        for(var i=0; i<data.length; i++) {
        //data[i] is an individual line of excel data
            var lin = data[i]["Nr SAT"];



            $('body').append($('<p>').html('Numero Sat: '+ '<a href = http://sath3g.altervsta.org/jsonint.html+?id='+lin '>'+ data[i]["Nr SAT"]+ '</a>'));

            $('body').append($('<p>').html('Data: '+ data[i]["Data"]));
            //http://sath3g.altervsta.org/jsonint.html +?id=+lin

            }
        });
</script>

    <body>
        <header>

        </header>


    </body>

</html>

In the line of code below, I want to put in the link using variable lin. But it doesn't work.

$('body').append($('<p>').html('Numero Sat: '+ '<a href =http://sath3g.altervsta.org/jsonint.html+?id='+lin '>'+ data[i]["Nr SAT"]+ '</a>'));

What is wrong here?

Upvotes: 0

Views: 75

Answers (2)

Ankur Aggarwal
Ankur Aggarwal

Reputation: 3101

You are missing '+' after lin

$('body').append($('<p>').html('Numero Sat: '+ '<a href =http://sath3g.altervsta.org/jsonint.html+?id='+lin+'>'+ data[i]["Nr SAT"]+ '</a>'));

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337733

You're missing a + concatenation character after the lin variable. Try this:

$('body').append($('<p>').html('Numero Sat: <a href ="http://sath3g.altervsta.org/jsonint.html+?id=' + lin + '">'+ data[i]["Nr SAT"] + '</a>'));

Better yet, build the DOM elements instead of concatenating ugly strings.

var $p = $('<p />');
var $a = $('<a />', { 
    href: 'http://sath3g.altervsta.org/jsonint.html+?id=' + lin,
    text: data[i]["Nr SAT"]
});
$('body').append($p.append($a));

Upvotes: 1

Related Questions