0

I am trying to loop through html content, for each html has a text input which i want to get the text value and pass it to the html span that also has id replyJail-number of the same with id of replybefore-number

HTML PART

<span id="replyJail-1" class="rep"></span>
<input id="replybefore-1" class="brp" type="hidden" value="hello"/>

<span id="replyJail-2" class="rep"></span>
<input id="replybefore-2" class="brp" type="hidden" value="#Bold letter"/>

<span id="replyJail-3" class="rep"></span>
<input id="replybefore-3" class="brp" type="hidden" value="`My code`"/>

Javascript

var x = document.getElementsById(id);
//x[i]
var i;
for (i = 0; i < x.length; i++) {
  var n = i++;
  var converter1 = Markdown.getSanitizingConverter();
  var CODESOFTLAB = new Markdown.Converter(converter1);
  var before = document.getElementById("replybefore"+n).value;
  var MarckDowPreviewHtml = CODESOFTLAB.makeHtml(before);
  document.getElementById("replyJail"+n).innerHTML = MarckDowPreviewHtml;
  var a = 10;
}
4
  • 2
    getElementById--(Not getElementsById as itdoes not exist in JavaScript) returns only single element not collection...You have perfect markup to use getElementsByClassName/querySelectorAll Commented Jul 26, 2016 at 14:27
  • 1
    var x = document.getElementsById(id); is wrong, the right way var x = document.getElementById(id); Commented Jul 26, 2016 at 14:28
  • 1
    @Rayon: getElementsById isn't a function. It's getElementById - no "s". Commented Jul 26, 2016 at 14:30
  • 1
    var n = i++; increments i an exrta time too (so your loop increments in 2s), you should probably use n = i + 1 Commented Jul 26, 2016 at 14:34

1 Answer 1

1

You can get all input elements using getElementsByClassName. Now, iterate through each input and get the corresponding span's id by replacing "before" with "Jail". You can now insert the corresponding input's value in the target span as textContent.

var inputs = document.getElementsByClassName("brp");
var converter1 = Markdown.getSanitizingConverter();
var CODESOFTLAB = new Markdown.Converter(converter1);

for( var i = 0; i < inputs.length; i++ )
{
  var input = inputs[i];
  var value = input.value;
  var MarkDownPreviewHtml = CODESOFTLAB.makeHtml(value);
  var targetId = input.id.replace("before", "Jail");
  var targetSpan = document.getElementById( targetId );
  // targetSpan.textContent = MarkDownPreviewHtml;
  targetSpan.innerHTML = MarkDownPreviewHtml;
}//for()


<span id="replyJail-1" class="rep"></span>
<input id="replybefore-1" class="brp" type="hidden" value="hello"/>

<span id="replyJail-2" class="rep"></span>
<input id="replybefore-2" class="brp" type="hidden" value="#Bold letter"/>

<span id="replyJail-3" class="rep"></span>
<input id="replybefore-3" class="brp" type="hidden" value="`My code`"/>
Sign up to request clarification or add additional context in comments.

4 Comments

how am i going to include this var converter1 = Markdown.getSanitizingConverter(); var CODESOFTLAB = new Markdown.Converter(converter1); it is my markdown converter it worked but doesn't convert any more
@Alex I have updated the code to include markdown converter code from your sample code.
Bahit what is the different between innerHTML and textContent i this i printing the html content inside the text value as text instead of html can you change that?
@Alex updated the code. Please check if that is what works for you.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.