0

I'd like to change the tag while maintaining the attributes within said tag then have one of the values of the attribute be used within the textarea.

Here's a sample code:

<tbody>
   <tr>
      <td someClass>
         <input attrOne="one" attrTwo="two" value=""></input>
      </td>
   </tr>
</tbody>

Here's my code that replaces the tag and keeps the attributes:

<script>
   jQuery('.someClass input').replaceWith(function () {
     return this.outerHTML.replace("<input", "<textarea").replace("</input", "</textarea")
   });
</script>

It's a form that allows users to type in anything and the 'value' attribute becomes what they type in. When I look at the source code, the value shows up fine. But the value of 'value' attribute is not being input into the textarea.

Here's what I tried but gives me blank:

<script>
jQuery('.someClass').children('textarea').text(jQuery('.someClass textarea').attr("value"));
</script>

How would I go about grabbing the value of 'value' attribute and putting it within the textarea?

Andrew Chu
  • 23
  • 5
  • 1
    You shouldn't define custom attribuets. If you need application-specific attributes, use `data-XXX`. – Barmar Oct 18 '21 at 20:48
  • Can you create a [mcve] that demonstrates the problem? – Barmar Oct 18 '21 at 20:54
  • How are you setting `value`? Assigning to `.value` doesn't change the `value` attribute, and it doesn't appear in `.outerHTML`. The value attribute is the initial default value, not the current dynamic value. – Barmar Oct 18 '21 at 20:56
  • You need to use `.setAttribute("value", "newvalue")` to change the attribute. – Barmar Oct 18 '21 at 20:56
  • And when you create a textarea, is initial contents is in the `innerHTML`, not the `value` attribute. – Barmar Oct 18 '21 at 20:57
  • @Barmar I'm not trying to change the attribute name. The value of 'value' is blank on load but when a user enters something within the input, 'value' is set to whatever the user entered. Looking at some other questions, it seems like textarea doesn't have a value attribute but input does so maybe that's the problem right now? – Andrew Chu Oct 18 '21 at 21:02
  • When someone types into an input, it doesn't change the value attribute, only the value property. – Barmar Oct 18 '21 at 21:03
  • Type something into the input, then inspect the element in DevTools. You'll see that the value attribute hasn't changed – Barmar Oct 18 '21 at 21:04
  • Ah okay, sorry that's what I meant then. Trying to get the value property to be inputted into the text area. – Andrew Chu Oct 18 '21 at 21:06
  • BTW, there's no `` in HTML, since `` is not a container element. – Barmar Oct 18 '21 at 21:07

0 Answers0