Question

I want to find number and append to a div:

$(document).ready(function () {

    var $numb = $('#my').html().match(/\d+/g);
    $numb.appendTo('#result');
});

for testing i add a alert, this work fine but append not working. here is JSFiddle

what's the problem?

Was it helpful?

Solution

since numb is a numerical value, you need to use .append() to append it to the target element like

var numb = $('#my').html().match(/\d+/g)[0];
$('#result').append(numb);

Demo: Fiddle

OTHER TIPS

$numb is not jQuery Object it's just a simple variable which contains number .

you can't apply appendTo() with variables you need jQuery Object.

You need

$('#my').html().match(/\d+/g) return array so you need to get the first value of the array so change it to $('#my').html().match(/\d+/g)[0] . as index starts from 0 .

.append()

var $numb = $('#my').html().match(/\d+/g)[0];
$('#result').append($numb);

Fiddle Demo

I might do it the other way round

var $numb = $('#my').html().match(/\d+/g);

$('#result').append($numb.toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top