html - append value of a javascript variable to a label without script - Stack Overflow

Is there a way to append the value of a javascript variable to a label without using jquery?Eg:Var glob

Is there a way to append the value of a javascript variable to a label without using jquery?

Eg:

Var global_message = "1234";

<label id="my-label">Test</label>

I want the value Test1234 to be displayed on the browser.

I dont want to write a jquery to say

$("#my-label").val() = $("#my-label").val()+global_message

Is there a way to append the value of a javascript variable to a label without using jquery?

Eg:

Var global_message = "1234";

<label id="my-label">Test</label>

I want the value Test1234 to be displayed on the browser.

I dont want to write a jquery to say

$("#my-label").val() = $("#my-label").val()+global_message
Share Improve this question asked Jan 24, 2017 at 7:07 jothijothi 3721 gold badge5 silver badges16 bronze badges 2
  • The title says "without script"? You can't get a value of JS variable without a script. What is the real point here? – Teemu Commented Jan 24, 2017 at 7:12
  • Are you asking for document.getElementById('my-label').innerHTML += global_message – mseifert Commented Jan 24, 2017 at 7:13
Add a ment  | 

3 Answers 3

Reset to default 2

What I understand from your question is that you want to access JavaScript variable directly in HTML and don't want to use any JavaScript/jQuery code.

But, You can not access JavaScript variable outside <Script> tag. HTML does not recognise any variable.

So you can use @WitVault answer if you want to use JavaScript and @Pranav C Balan using jQuery

var global_message = "1234";

document.getElementById("my-label").innerText += global_message;
<label id="my-label">Test</label>

The val() method is using to update the form elements value so it can't be used for updating text content of the label.

For updating label text use text() method with a callback function, where the second argument in callback holds the old text.

$("#my-label").text(function(i, oldText){
  return oldText + global_message;
})

var global_message = "1234";

$("#my-label").text(function(i, value) {
  return value + global_message;
})
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<label id="my-label">Test</label>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745435874a4627604.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信