To fix your code:
-
Add the
event
function doit_onkeypress(a, event) {
…
<p id="para" tabindex="0" onkeypress="javascript: doit_onkeypress(this.id, event);">
-
Add the id into the jQuery selector
$("#" + a + ".token").each(function () {
-
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>
2
solved Get Value in JavaScript [closed]