0

How can i change name of input, if is my DOM saved in JS variable?

var data = '<input id="layout_name" name="link" type="text" value="">';

i need change value of parameter name to link[1] (im using jQuery library in this project)

Thank you in advance!

4
  • 1
    Don't save it as a text value. Use DOM elements in JS. Commented Jun 3, 2014 at 14:54
  • Here's a reference: developer.mozilla.org/en-US/docs/Web/API/element Commented Jun 3, 2014 at 14:55
  • data = $(data).prop('name', 'link[1]').get(0).outerHTML; -> jsfiddle.net/354q3 Commented Jun 3, 2014 at 14:56
  • ^^ and manipulating HTML string like this is generally a bad idea ! Commented Jun 3, 2014 at 14:57

2 Answers 2

4
var data = '<input id="layout_name" name="link" type="text" value="">';
var $input = $(data);
$input.attr('name', 'other');
data = $input[0].outerHTML;
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. is there a way to do just with the 'data' variable ( without using $input) ?
0

You can do it as follows:

var data = $('<input id="layout_name" name="link" type="text" value="">');
data.attr('name', 'new_value');

You can create HTMLElements directly with jQuery

Comments

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.