Skip to main content

Maintaining dynamic textbox control values into an array in javascript onchange event



I need to maintain text box values into an array in javascript onchange event. The text box control is created dynammically in javascript function. I have used below code. But at the end the array contains current element only. Previous values are not maintaining. How can I maintain textbox value each time entered.







<script type="text/javascript" language="javascript">

function DrawTextBox(j) {

var imgElement = document.getElementById('myimage');

var imgCoord = imgElement.getBoundingClientRect();

var cl = imgCoord.left + 20;

var x = x - cl;

var y = y - imgCoord.top;

var rect = document.getElementById(j);

if (rect == null) {

rect = document.createElement("div");

rect.id = j;

var pElement = document.getElementById("imgDiv");

pElement.appendChild(rect);

}

if (w < 0) {

w = 10;

}

if (h < 0) {

h = 10;

}

var l = parseInt(document.getElementById("hright").value) + 150 + 'px';

var style = rect.style;

// style.width = w + "px";

//style.height = h + "px";

style.left = l;

style.top = document.getElementById("htop").value;

style.backgroundColor = "Transparent";

//style.borderColor = "Blue";

style.position = "absolute";

style.borderStyle = "solid";

style.borderWidth = 2 + "px";

style.zIndex = 6000;

document.getElementById(j).innerHTML = "<textarea name=\"comments\" style=\" border:0\" Title=\"Enter Comment\" id=\"T1\" value=\"EnterComment:\" onchange=\"GetText()\" ></textarea>";

//document.getElementById(i).innerHTML = "<input type=\"Textarea\"...blah blah...></input>";

return rect;

}



function GetText() {

array1.push(document.getElementById('T1').value);

alert(array1.join(', '));

}

</script>




Comments

  1. Your question doesn´t say if/when/where array1 is declared and when DrawTextBox() is called but you can´t insert multiple elements using the same ID "T1" as it violates basic HTML rules.

    Try passing a referens to the current element like this onchange="GetText(this)" and use it in your GetText() function as;

    function GetText(el) {
    array1.push(el.value);
    alert(array1.join(', '));
    }

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.