Trying to access javascript elements inside a page

Hope someone can help me. I am trying to access a textbox inside a webpage, so I can make some scripts for example. placing text in fields, validating a field and clicking a button, automating the workflow of my employees. This is confusing because I cannot find the name / id / anything that will allow me to manipulate the form. I can see the field name I am trying to use Firebug ("history [comment]") and id if it helps ("history_comment"), but no matter what I do, the form will not be processed, Based on other scripts. which I did, this Applescript:

do JavaScript "document.forms[1].history_comment.value='Testing';" in document 1

      

should complete the task, telling the browser to put "Testing" in the appropriate box. I have replaced other names that I think might be what he wants and tried to refer to any other forms (forms [2], forms [3]), all to no avail. Actually I'm a little confused because no assertions exist in HTML, so maybe I am bringing in there.

I posted an HTML dump of the form to http://images.jlist.com/testform.html (with dummy information of course), if any soul can grab a gander and give me some direction. My goal is to provide information in the "Comment" field. Can I run a script that will tell me the full name (as far as the browser is concerned) of each element on the form?

0


source to share


4 answers


if you can use jquery then you can do it quite easily using the following command



$("history_comment").val("HELLO");

      

+1


source


JavaScript should be:

document.getElementById("history_comment").value='Testing';

      



document.forms is non-standard and just like your example code will fail if the element is not inside the form. This is fairly common in AJAX applications and is another good reason to avoid document.forms.

+1


source


What @Kikuchyo wrote, although in fact it is strictly wrong not to wrap form elements like textarea

in a tag form

. You will also need a tag form

if (as you suggest) you want to submit the form programmatically. Since you are already accessing this text field, you can get the form from a javascript function:

var thetext=document.getElementById('history_comment');
thetext.value='whatever you want to put in there';
thetext.form.submit(); // all form elements have a 'form' property

      

You can get the status of a checkbox document.getElementById('history_notify').checked

; this is a boolean value, so set it to true

or false

and use it directly in conditionals.

Of course, if (since looking at the form you most likely want) you want to submit AJAX, you will need to check the documentation for whatever wrapper library you are using.

0


source


since your element is a text area, it should be done like this:

document.getElementById ('history_comment'). innerHTML = 'HELLO';

using innerHTML instead of value

-1


source







All Articles