corning
corning

Reputation: 25

How can I change the name of an input element with jQuery?

I know I can target an input element with a specific name, for example

$("textarea[name=form_textarea]").val();

However I can't seem to find any examples of how I could go about changing the name itself, is this possible?

Upvotes: 0

Views: 34

Answers (1)

Steve Robbins
Steve Robbins

Reputation: 13812

$("textarea[name=form_textarea]").attr("name", "new_name");

$("textarea").val("Name is: " + $("textarea").attr("name"));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea name="form_textarea"></textarea>

Upvotes: 2

Related Questions