Reputation: 11330
I have a textarea with code of form which the users would copy and paste into their website.
The html in the textarea would be something like this
UPDATE : Note the below form is enclosed in a textarea. This would a plain text in a textarea
<form action="https://www.xxxxxxx.com/servlet/servlet?encoding=UTF-8" method="POST">
<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>
<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>
<input type="submit" name="submit">
</form>
I want to change the url of the form tag to https://www.yyyyyyyyy.com/parse.php and copy the changed form code into another textarea using jquery or normal javascript. Below is the full html
<form>
<textarea rows="10" cols="80" id="oldCode">
<form action="https://www.xxxxxxx.com/servlet/servlet?encoding=UTF-8" method="POST">
<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>
<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>
<input type="submit" name="submit">
</form>
</textarea>
<button onclick="generateCode()">Generate new Code</button>
<textarea id="newCode" rows="10" cols="80">
</textarea>
</form>
Upvotes: 0
Views: 763
Reputation: 23416
Here's a simple snippet:
$('button').click(function () {
var clone = $($('#oldCode').val()).clone();
clone.attr('action', 'https://www.yyyyyyyyy.com/parse.php');
$('#newCode').val(clone[0].outerHTML);
});
I.e. just create a clone of the value of the textarea, change the attribute, and add clone's HTML to the other textarea.
Upvotes: 1
Reputation: 28523
You can do it in following way, put type="button"
for button element as this submits forms everytime
<button type="button" id="generate" >Generate new Code</button>
use below jQuery where bind click event to button and replace action attribute of form.
Note :- you can call onclick javascript function with same code instead of biniding click event
$(function(){
$("#generate").click(function(){
var value = $('#oldCode').val();
var div = $('<div></div>');
$(div).append(value);
$(div).find('form').attr('action','https://www.yyyyyyyyy.com/parse.php');
$('#newCode').val( $(div).html());
});
});
Upvotes: 2
Reputation: 8367
Give an id
to the form.
<form action="https://www.xxxxxxx.com/servlet/servlet?encoding=UTF-8" method="POST" id="form">
^
<label for="first_name">First Name</label><input id="first_name" maxlength="40" name="first_name" size="20" type="text" /><br>
<label for="last_name">Last Name</label><input id="last_name" maxlength="80" name="last_name" size="20" type="text" /><br>
<input type="submit" name="submit">
</form>
And in the jquery function
$('#form').attr('action', 'https://www.yyyyyyyyy.com/parse.php '); // set the action attribute to required page
$('#newCode').html($('#form').html()); // set the html of textarea with id newCode as the html content of form with id form
Upvotes: 0
Reputation: 3090
Add id
to your form
say myForm
,
$('button').click(function(){
var formClone = $('#myForm').clone();
formClone.attr('action', 'https://www.yyyyyyyyy.com/parse.php');
$('#newCode').text(formClone);
)};
Upvotes: 0