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
سؤال
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?
المحلول
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
نصائح أخرى
$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
.
var $numb = $('#my').html().match(/\d+/g)[0];
$('#result').append($numb);
I might do it the other way round
var $numb = $('#my').html().match(/\d+/g);
$('#result').append($numb.toString());