1

I have this variable in javascript:

var datos = '<input type="hidden" name="i_txt_Prod_Code" value="Foo_Bar" tabindex="1">01080';

I need get the text 01080 from this, ignoring the hidden input or any other input tag.

var text = data.text(); //I used this but it did not work

if (data.match("<input")) {
  var datos = $(data);
  datos.find("input[type=hidden]").each(function(index) {
    $(this).remove();
  });
  data = datos.html();
}

not duplicated @HereticMonkey pls read the post first. i am using jquery no javascript pure. o EMC structure.

10
  • Why can you not use the hidden input? If the value of that always matches the text you want to retrieve, it would be far easier to just read it from there. Commented Apr 17, 2019 at 15:15
  • @RoryMcCrossan becouse They do not always coincide; What I need is to resolve the doubt, not do something different. Commented Apr 17, 2019 at 15:21
  • In which case giving them the same value in the question is confusing matters. I've edited the question to remove that value. In any case, see my answer below Commented Apr 17, 2019 at 15:22
  • Possible duplicate of Strip HTML from Text JavaScript Commented Apr 17, 2019 at 15:29
  • not duplicated @HereticMonkey pls read the post first. i am using jquery no javascript pure. o EMC structure. Commented Apr 17, 2019 at 15:39

1 Answer 1

3

Assuming you cannot access the input, for whatever reason, the simplest way to achieve what you require would be to create a jQuery object which is a div that contains the HTML you have in the datos variable. Then you can simply call text() on this div:

var datos = '<input type="hidden" name="i_txt_Prod_Code_01080" value="01080" tabindex="1">01080';

var $datos = $('<div>' + datos + '</div>');
console.log($datos.text());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.