0

I have :

<span class="badge" id="total">10</span>

If I using jquery :

$('#total').text()

How to get it if using vue.js 2?

3
  • 1
    If you don't want to use jQuery, you can use javascript : document.getElementById('total'); or document.querySelector("#total"); If you want to use Vue.js you can use <span class="badge">{{ total }}</span> and put total in data or computed properties Commented Mar 29, 2017 at 13:07
  • 1
    With any library using virtual DOM you will not want to read/write directly to the DOM. If this is just the read operation, go ahead with what @SLYcee suggested. Instead if you will want to change that value, making it into an state is the right way to do it. So that you just change the state and everything happends automatically! Commented Mar 29, 2017 at 13:16
  • 1
    These should be answers, not comments. Commented Mar 29, 2017 at 13:37

1 Answer 1

5

Why does your HTML look like that?

You should never need to do what you're asking in a Vue application. With a MVVM application you'd have something like:

new Vue({
  data: {
    total: 10,
  },
})

Then a template like:

<span class="badge" id="total">{{ total }}</span>

That way, in your VM, when you set this.total = 12 the DOM will update automatically and this.total will always return your total.

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

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.