0

Is it possible to print a variable alongside text using .text?

This is my code currently but it doesn't seem to work

var rowCount = $('#result li').length;
$('#count').text((rowCount)"results for");

thanks for your help

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Danzel91
  • 113
  • 9
  • 1
    `$('#count').text(rowCount+" results for")` or using ES6 template literals `$('#count').text(\`${rowCount} results for\`)` – connexo Nov 18 '18 at 00:53
  • @connexo nice one, thank you – Danzel91 Nov 18 '18 at 00:54
  • What is jquery? – duhaime Nov 18 '18 at 00:58
  • 1
    Note that this isn't really about how `.text()` works, it's about how to concatenate values together. So the same solutions will apply for use in other situations including calling other functions, variable assignments, etc. – nnnnnn Nov 18 '18 at 01:16
  • 2
    You can even do both in one line: `$('#count').text(\`${$('#result li').length} results for\`)` – connexo Nov 18 '18 at 01:17
  • Possible duplicate of [How to interpolate variables in strings in JavaScript, without concatenation?](https://stackoverflow.com/questions/3304014/how-to-interpolate-variables-in-strings-in-javascript-without-concatenation) – Mohammad Nov 18 '18 at 09:26

2 Answers2

1

Try to use this :

var rowCount = $('#result li').length;
alert($('#count').text(rowCount+"results for"));
Samir Guiderk
  • 187
  • 2
  • 4
  • 17
1

Yes sure it's possible. If I understand you correctly you want the variable printed along with some text.

var rowCount = $('#result li').length;
$('#count').text(rowCount + " results for");

or es6

var rowCount = $('#result li').length;
$('#count').text(`${rowCount} results for`);
Samson Iyanda
  • 512
  • 3
  • 13