Event-driven

Yesterday I had to type a value in some textboxes in a web application. It was time for a Greasemonkey script. The stripped version of it is:

var valButton = document.createElement("input");
valButton.type = "text";
valButton.value = "10";
document.body.appendChild(valButton);
var btn = document.createElement("BUTTON");
var t = document.createTextNode("POPULATE");
btn.appendChild(t);
btn.addEventListener('click', function(){
var x = document.getElementsByClassName("dxeEditArea");
  alert(x.length);
var i;
for (i=0; i< x.length; i++)
  {x[i].value=valButton.value;}
});
document.body.appendChild(btn);

So what’s the valButton.value in line 13 since it is “10” in line 3? What line 3 actually does is not setting THE value but the default value. Actually, the user may have changed it before the onClick event.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s