Knockout JS - Value of span

606 views Asked by At

My application is MVC5; I am trying to get the value of Knockout span using the following:

<span id="total" data-bind="text: allImages().length"> </span>

I see 10 on the screen which is the correct value.

Tried to use:

var total = $('.total').text();
alert(total);

Don't get any value, also tried .val().

Edit I can get the value if I use this function:

   function getvalue() {
        var total = $('#total').text();
        $('#mytext').val(total);
        alert(total);
    }

Is there a way to get the value Onchange of span text?

1

There are 1 answers

1
Rylab On

You need $('#total'), not $('.total')

#total finds that element by its ID, which is correct for your HTML span with that ID; .total means get by className, and there is no div in your code with that class, so it's correctly matching no elements.