[Solved] Get Value in JavaScript [closed]

Introduction

JavaScript is a powerful scripting language that can be used to create dynamic webpages and applications. It is often used to manipulate data and create interactive elements on a webpage. One of the most common tasks in JavaScript is to get the value of a variable or object. This can be done using the built-in functions provided by JavaScript. In this article, we will discuss how to get the value of a variable or object in JavaScript. We will also discuss some of the best practices for getting values in JavaScript.

Solution

You can use the dot notation or bracket notation to get the value of a property in JavaScript.

For example, if you have an object called “person” with a property called “name”, you can get the value of the “name” property using either of the following:

// Dot notation
let name = person.name;

// Bracket notation
let name = person[‘name’];

To fix your code:

  1. Add the event

    function doit_onkeypress(a, event) {
    

    <p id="para" tabindex="0" onkeypress="javascript:  doit_onkeypress(this.id, event);">
    
  2. Add the id into the jQuery selector

    $("#" + a + ".token").each(function () {
    
  3. Add a tabindex to paragraph, so that it can be focused.

This is how it works for me:

<html>
<head>

</head>
<body>
<p id="para" tabindex="0" onkeypress="doit_onkeypress(this.id, event);">
    <span class="token">test1</span>
    <span class="token">test2</span>
    <span class="token">test3</span>
</p>

<p id="para2" tabindex="1" onkeypress="doit_onkeypress(this.id, event);">
    <span class="token">test4</span>
    <span class="token">test5</span>
    <span class="token">test6</span>
</p>
<script>
function doit_onkeypress(a, event) {
    debugger;
    if (event.keyCode == 13 || event.which == 13) {
        alert(a);
        var splitdata;
        span_array = [];//define array

        $(".token").each(function () {// iterate over same class spans
            console.log($(this).text()); // print the text of each span
            span_array.push($(this).text());// push span text to array
        });
        console.log(span_array); // you can save data an array for further use
        var final_string = span_array.join(); //join array value as string
        console.log(final_string); // check string 
        splitdata = final_string
        document.getElementById("hdnvaluearray").value = final_string;
        document.getElementById("hdnvaluearray").value = a;
    }
}
</script>
</body>
</html>