JSP javascript function onclick to different jsp page - forms

I have a form and javascript function to open 2.jsp page but it keeps giving me
Message: Object doesn't support this property or method
Here is how I do it. I tried to put in only the important parts.
<form name = iForm action=1.jsp method=POST>
<input type="button" value="Button2" name=button2 onclick="OnButton1()"/>
</form>
<script language="JavaScript">
function OnButton1{
document.iFrom.action = "2.jsp";
document.iFrom.target = "_blank";
document.iFrom.submit();
return true;
}
< /script>
I know a way to open through 'window' but I was wondering if there is this way

You did not put parenthesis after your JavaScript function name and you misspelled the form name in your script.
Correct those mistakes and it should work as you might expect.
As well, the target attribute does work for forms.

Related

How to get widget property directly in java script in adobe cq5 ?

I have a widget in which my property name is "./title". i want to get this property value in javascript. for this i am getting its value in jsp.
JSP code
<div class="title">
<%=properties.get("./title","PageTitle")%>
</div>
jquery
$('.title').text();
By doing i'm able to get the value of title.But is there any other way through which i can get its value directly in javascript . thanks in advance.
You can initialize a JS variable with this value.
<script>
var title = "<%=properties.get("./title","PageTitle")%>"
</script>

How to dynamically add a script to the page using watir-webdriver

I have some *.js script that I want to execute using watir-webdriver during my test runs. So the question is there any way to upload the script to the page using watir-webdriver?
You could use the execute_script method to add a script element with the src attribute as your js file. The method call would look like:
browser.execute_script(
"var the_script = document.createElement('script');
the_script.setAttribute('src','your_script.js');
document.head.appendChild(the_script);"
)
To see that it works, let us assume you have the page:
<html>
<body>
<input type="text" id="field" value="100">
</body>
</html>
If you try to execute jQuery on the page (the '$' in the script), an exception occurs because the page does not know what jQuery is:
field = browser.text_field
p browser.execute_script('return $(arguments[0]).val();', field)
#=> $ is not defined (Selenium::WebDriver::Error::JavascriptError)
If you add the jQuery script file (http://code.jquery.com/jquery-1.10.2.js), via execute_script, you will now be able to use jQuery:
browser.execute_script(
"var the_script = document.createElement('script');
the_script.setAttribute('src','http://code.jquery.com/jquery-1.10.2.js');
document.head.appendChild(the_script);"
)
field = browser.text_field
p browser.execute_script('return $(arguments[0]).val();', field)
#=> "100"

How to make a script work for specific form on a page with mulitple forms

I am using a script that disables the submit button unless the user clicks and browses for a file to upload.
$(document).ready(function() {
$('input:submit').attr('disabled',true);
$('input:file').change(
function(){
if ($(this).val()){
$('input:submit').removeAttr('disabled');
}
else {
$('input:submit').attr('disabled',true);
}
});
});
I have 2 forms on the same page and I would like this tied to a specific form.
First form:
<form action="" method="post" name="name1" id="id1">
Second form:
<form action="" method="post" name="name2" id="id2">
is there a way in the javascript to attach it to #id1 specifically and not id2?
would I place it like this? $('#id1.input:submit') - what is the symbol or syntax to add the id into the script? Sorry I'm still learning which parts you can combine.
replace $('input:submit').attr('disabled',true); by
$('form#id1 input:submit').attr('disabled',true);

how to pass html form data to variable (nodejs / javascript)

so i'm using nodejs and nodejsdb-mysql. i want to make form for adding and searching posts.
I make html page with form, transform it to node using jsdom but how do i set up variables?
for example my html form is like:
name = text input
surname = text input
submit
and how do i pass inserted name/surname to var??
You will need to turn your submit into a button with an event such as onclick - this might be what you are looking for:
<input type="button" value="Submit" id="submit" name="submit" onclick="submit()">
<script type="text/javascript">
function submit(){
var name = document.getElementById('**name**').value;
var surname = document.getElementById('**surname**').value;
alert("Thanks for submitting, " +name);
}
</script>
With nodejsdb-mysql sooner or later you will face real problems like date localization problems and so on, use https://github.com/felixge/node-mysql instead

get checkbox group values

This has driven me really bananas. It's so simple and easy and yet I can't figure out what's wrong with it.
I want to get my checkbox value populated in my controller (for testing purposes).
Here is my form.
<a href='#' name='submitForm'>submit the form</a>
//I have jquery attached to this tag and will submit the form when user clicks it
echo form_open('test/show');
echo form_checkbox('checkbox[]','value1');
echo form_checkbox('checkbox[]','value2');
echo form_checkbox('checkbox[]','value3');
echo form_checkbox('checkbox[]','value4');
echo "<input type='text' name='text1' value='ddd'>";
echo form_close();
//My controller test
public function show(){
$data1=$this->input->post('text1');
//I can get text1 value from input box
$data2=$this->input->post('checkbox');
//it keeps giving me undefined index 'checkbox'
$data3=$_POST['checkbox'];
//same error message
//WTH is going on here!!!!!
}
Please help. This thing drives me nuts! Thanks.
UPDATE:
Thanks for the help. To be more precisely, my submit button is a <a> tag and outside of form tag. It appear that I have to include <a> tag inside my form tag to make them works. Is that true?
A checkbox will not submit any data if it is unchecked as they're not considered successful (as per the w3c specification here)
If you actually tick the box and submit, it'll work - in fact it does, I've just tested it.
You need to wrap calls to $_POST in the isset() function.
if( isset( $_POST['checkbox'] ) ) {}
Calling $this->input->post('checkbox') shouldn't give you an undefined index error as the method deals with this eventuality. the Input::post() method returns false or the value of the checkbox.
Edit --
In response to your amendment to your question, you must use an element of type input with the type attribute set to submit in order to submit your form data without the use of Javascript etc. This button must be INSIDE the <form></form> which you are intending to submit.
<input type="submit" value="Submit">
The type="submit" causes the browser to send the data as submit event occurs. If you wish to use another element insider or outside of the form to do this you need to use Javascript. This however can be disabled on a per browser/user basis and isn't reliable as a result.
// Standard Javascript
<form name="myform"...
<a onclick="javascript:document.myform.submit();" href="javascript:void(0)">Submit</a>
// jQuery
$('#my-a-tag-submit-button').live( 'click', function() {
$('#my-form').submit();
}