1

good day. how can i modify the text of textarea input using javascript

i have this content in my textarea, i want to remove the PublisherImprintName tag using jquery

  <PublisherInfo>
    <PublisherName>Ask question</PublisherName>
    <PublisherLocation>ph</PublisherLocation>
    <PublisherImprintName>ask stackoverflow</PublisherImprintName>
    <PublisherURL>stackoverflow.com</PublisherURL>
  </PublisherInfo>

and the posible output is

<PublisherInfo>
    <PublisherName>Ask question</PublisherName>
    <PublisherLocation>ph</PublisherLocation>
    <PublisherURL>stackoverflow.com</PublisherURL>
  </PublisherInfo>

is it possible using jquery?

i searched and i found this code, but i can't apply this to my problem, but the solution is almost similar

<script>
$(document).ready(function(){
    $("button").click(function(){
        $("PublisherImprintName").remove();
    });
});
</script>

thank you. sorry for asking too many questions

another question:

how can i remove tag name but retain its value?

i found a solution about that here: i tried applying it on my problem, but i can't make it work

Remove a HTML tag but keep the innerHtml

here is the text on my text area

  <PublisherInfo>
    <PublisherName>Ask question</PublisherName>
    <PublisherLocation>ph</PublisherLocation>
    <PublisherImprintName>ask stackoverflow</PublisherImprintName>
    <PublisherURL>stackoverflow.com</PublisherURL>
  </PublisherInfo>

and the possible output

<PublisherName>Ask question</PublisherName>
    <PublisherLocation>ph</PublisherLocation>
    <PublisherImprintName>ask stackoverflow</PublisherImprintName>
    <PublisherURL>stackoverflow.com</PublisherURL>
3
  • Please post a functional example that illustrates your problem on jsFiddle. That would really helps us to figure out the issue with your code. Thanks! Commented Jul 18, 2016 at 5:31
  • i don't have any functional code yet. what i have is a html code. i dont have a jquery code yet. is it possible? ive read here that it is possible using php php.net/manual/en/function.strip-tags.php . but as of now if its possible using jquery i prefer to use jquery Commented Jul 18, 2016 at 5:37
  • are you saying that the publisher tags where inside the textarea? Commented Jul 18, 2016 at 5:37

3 Answers 3

2

try this

var value = $("textarea").val();
value = value.replace(/<PublisherImprintName( |>).*?<\/PublisherImprintName>/gi,"");
$("textarea").val( value );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea>
  <PublisherInfo>
    <PublisherName>Ask question</PublisherName>
    <PublisherLocation>ph</PublisherLocation>
    <PublisherImprintName>ask stackoverflow</PublisherImprintName>
    <PublisherURL>stackoverflow.com</PublisherURL>
  </PublisherInfo>
</textarea>

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

2 Comments

that code works: i updated the question above sir. i have another question. can you take a look? thank you so much. your a life saver
@askquestionzero what updates have you made? Please point them out. Also, it is not advisable to ask multiple questions in the same question as it becomes confusing for the future visitors.
1

You can do something like this,

$(function() {
  function removeNode(str, nodeName) {
    var pattern = '<'+nodeName+'>[\\s\\w]+<\/'+nodeName+'>';
    var regex = new RegExp(pattern, 'gi');
    return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');
  }
  
  $("#myBtn").on('click', function () {
    var txt = $("#txtArea").text();
    var newText = removeNode(txt, 'PublisherImprintName');
    $('#txtArea').text(newText);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="txtArea" rows="10" cols="50">
  <PublisherInfo>
    <PublisherName>Ask question</PublisherName>
    <PublisherLocation>ph</PublisherLocation>
    <PublisherImprintName>ask stackoverflow</PublisherImprintName>
    <PublisherURL>stackoverflow.com</PublisherURL>
  </PublisherInfo>
</textarea>
<button id="myBtn">Button</button>

Edit:

For your another question, you can do this..

$(function() {
  function removeNodeButRetain(str, nodeName) {
    var pattern = '<\/?'+nodeName+'>';
    var regex = new RegExp(pattern, 'gi');
    return str.replace(regex, '').replace(/^\s*[\r\n]/gm, '');
  }
  
  $("#myBtn").on('click', function() {
    var txt = $("#txtArea").text();
    var newText = removeNodeButRetain(txt, 'PublisherInfo');
    $('#txtArea').text(newText);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="txtArea" rows="10" cols="50">
  <PublisherInfo>
    <PublisherName>Ask question</PublisherName>
    <PublisherLocation>ph</PublisherLocation>
    <PublisherImprintName>ask stackoverflow</PublisherImprintName>
    <PublisherURL>stackoverflow.com</PublisherURL>
  </PublisherInfo>
</textarea>
<button id="myBtn">Button</button>

3 Comments

i have another question sir. kindly help me.
thank you for you patience. ill create anothe thread. and what is the reason sir that i shoudn't? one question per thread?
@askquestionzero As gurvinder mentioned, it'd help future readers. Moreover, the previous answers before your question update will look irrelevant.
0

search attribute in jquery .By using the attribute functionality of jquery u can remove that https://api.jquery.com/attribute-contains-selector/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.