0

I know that document.createTextNode(text) in JS is for creating text only, but is there a way to insert into it two span elements?

I have two spans created like this:

/*
   *   span with wrong (old) medical word
   */

  // Replacement node
  var span_old = $('<span />', {
    'class': pluginName + '-autoword-highlight wrong wrong-medical-word-'+i
  });

  // If we have a new match
  if (i !== c) {
    c = i;
    replaceElement = span_old;
  }

  span_old
  .text(fill)
  .data({
    'firstElement': replaceElement,
    'word': oldWord
  });




  /*
   *  span with new - corrected medical word
   * 
   */
  // Replacement node
  var span_new = $('<span />', {
    'class': pluginName + '-autoword-highlight corrected-medical-word-'+i
  });

  // If we have a new match
  if (i !== c) {
    c = i;
    replaceElement = span_new;
  }

  span_new
  .text(replaceFill)
  .data({
    'firstElement': replaceElement,
    'word': replaceFill
  });

  var wrong_and_corrected_medical_text = span_old[0] + span_new[0];

  return document.createTextNode(wrong_and_corrected_medical_text);

Expected result is

old text - new text

and I need to return Text Node as always. Is there a hack to stringify the span elements or other way? Thanks!

user2670818
  • 719
  • 5
  • 12
  • 28

1 Answers1

1

Is this what you need?

var wrong_and_corrected_medical_text = span_old.text() + span_new[0].text();

Edit: So you need the html to be returned as text, Please try this.

var wrong_and_corrected_medical_text = span_old.prop('outerHTML') + span_new.prop('outerHTML');

Reference: https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML

Muthukannan Kanniappan
  • 2,080
  • 1
  • 16
  • 18
  • Almost. I need to keep the span tags, the example you are showing is just obtaining the text of the span and returning two strings, but I need two strings wrapped in spans. I thoung "oldnew-text" with the quotes would help, but cannot make it. – user2670818 May 24 '16 at 07:48