Solved

change form target with jquery

Posted on 2014-12-12
2
444 Views
Last Modified: 2014-12-15
Hello I have an html form with an onsubmit to validate the fields. If the form validates I have the form opening in a popup window.

function checkform()
{
 some validation

$('#form1').submit(function() {
        window.open('', 'formpopup', '');
        this.target = 'formpopup';
    });
}

function confirmPrint(theID)
{
 var sURL = "checkifprintedModal.cfm?myID="+theID;
showPopWin(sURL,600,375,null);
}
function loaddata()
{
 ajax to load table form

if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
		else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		xmlhttp.onreadystatechange=function()
		{
			if (xmlhttp.readyState==4 && xmlhttp.status==200)
			{
                             document.getElementById("mydata").innerHTML=xmlhttp.responseText;
                        }
                }
                xmlhttp.open("GET","getmystuff.cfm?accountID="+encodeURIComponent(accountID),true);
		xmlhttp.send();

}
<body onload="loaddata();">
 <form id="form1" target="_blank" onsubmit="return checkform()">
 <div id="mydata">
   <table>
     <!--- ajax data goes here --->
  <tr>
     <td> <input type="submit" value="Submit">
 </tr>
</table>
</div>  
<table>

</form>
</body>

Open in new window


So what I am doing to when the user clicks submit it does the check form. If that comes back with no validation issues the form opens a popup window that does some printing. On the popup page I have onload function that the confirmPrint function on the parent page to open up a modal window to ask if the pages printed properly. When the modal window closes I am calling the loaddata function to reload the page data. If the user does some other things on the main page the click submit and if the validation fails a blank tab opens. This is happening because I changed the form target to the formpopup. Is there a way I can change the form target back to what it was before.

I tried

 $('#form1').submit(function() {
      	$('#this').target="new";
    });

Open in new window

and
   var targetvalue = "new";
	   $('#form1').prop("target", targetvalue);

Open in new window


and
   $('#form1').submit(function() {
      	$('#form1.target')="new";
    });

Open in new window

with this I am getting js error saying "Uncaught ReferenceError: Invalid left-hand side in assignment"
 and quite a few more things in the loaddata and the checkform function and I can't get anything to work properly.
0
Comment
Question by:spectrumcare
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 40497451
Should work :
$('#form1').submit(function() {
      $(this).attr("target", "new");
});

Open in new window

0
 
LVL 8

Expert Comment

by:Naitik Gamit
ID: 40497640
<script type="text/javascript">
// Popup window code
function newPopup(url) {
      popupWindow = window.open(
            url,'popUpWindow','height=700,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes')
}
</script>
<a href="JavaScript:newPopup('http://www.quackit.com/html/html_help.cfm');">Open a popup window</a>
0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sort Multi-dimensional array 6 45
bootstrap form validations! 1 41
jQuery element reference issue 2 40
hide and show spans from dropdown selection 3 38
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question