0

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>

4 Answers 4

2

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());
    });
});

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

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.

A live demo at jsFiddle.

Comments

0

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);
)};

1 Comment

The form is with in a textarea. This form should be considered as text
0

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

1 Comment

The "form" in the question is plain text, not an HTML element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.