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>
Community
  • 1
  • 1
  • Please post a functional example that illustrates your problem on [jsFiddle](http://jsfiddle.net/). That would really helps us to figure out the issue with your code. Thanks! – palaѕн Jul 18 '16 at 05: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 http://php.net/manual/en/function.strip-tags.php . but as of now if its possible using jquery i prefer to use jquery – askquestionzero Jul 18 '16 at 05:37
  • are you saying that the publisher tags where inside the textarea? – Mark Vincent Manjac Jul 18 '16 at 05:37

3 Answers3

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>
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 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 Jul 18 '16 at 05:54
  • 1
    @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. – gurvinder372 Jul 18 '16 at 05:57
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>
choz
  • 17,242
  • 4
  • 53
  • 73
0

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

Varaj Vignesh
  • 341
  • 1
  • 7