0

I have a JavaScript function that returns selected

function getage() {
    var selectedAge = selectedYear;
    var selectedAge = "";
    console.log(selectedAge);
    for (var key in selectedAge) { // run for every value in selectedAge
        console.log(key);
        if (selectedBirth.indexOf('20-' + key) > -1){
            if (selectedAge[key].length > 0) { // check if the subarray of selectedAge contains a value (if birth has selected age)
                selectedAge = selectedAge + age[key] + " selectedAge"; // add the age code as well as 'selected age' to return string
            } else {
                selectedAge = selectedAge + birth[key]; // adds only birth name
            }    
            selectedAge = selectedAge + "\n" + "\t";
        }
    }
    return selectedAge;
}

Any help would be appreciated!

2
  • where do you want to display it? html? plain text? console? Commented Apr 1, 2016 at 8:51
  • 1
    Do not vandalize your question. Make or accept an answer if it's "fixed". Commented Apr 2, 2016 at 18:19

4 Answers 4

1

If you want a new line...

  • in your HTML code (or strings in general), use \n
  • in your HTML output, use the <br/> tag
  • in both your HTML code and its output, combine them (like <br/>\n)
Sign up to request clarification or add additional context in comments.

Comments

0

In html, either just use <br /> where you would have wanted to use \n, or wrap every item in a display: block element like div or p or li.

You then have to make sure that your html is not escaped before inserting into the page, by using a function like jQuery's .html() or element.innerHTML = "...";.

Comments

0

EDIT:

try to add this var breakline = document.createElement("br"); in the code and add it to your selectedString

function getCoverage() {
  var selectedMarkers = selectedTowns;
  var selectedString = "";
  var breakline = document.createElement('br');
  selectedString.appendChild(breakline);
  console.log(selectedMarkers);
  for (var key in selectedMarkers) { // run for every value in selectedMarkers
    console.log(key);
    if (selectedRegions.indexOf('ZA-' + key) > -1) { // if current province is one of the selected provinces
      if (selectedMarkers[key].length > 0) { // check if the subarray of selectedMarkers contains a value (if province has selected town)
        selectedString = selectedString + province[key] + " selected towns"; // add the province code as well as 'selected towns' to return string
      } else {
        selectedString = selectedString + province[key]; // adds only province name (since regional coverage for this province)
      }    
      selectedString = selectedString
    }
  }

  return selectedString;
}

7 Comments

please use <br/> instead of </br>
I've tried adding "<br/>". Doesn't work as well. Just prints out "<br/>" with string.
@Gijsberts Hi thanks but still not working only prints out "[object HTMLBRElement]" where the line break is suppose to be.
BTW <br/> is exactly the same as <br> in HTML5. According to the spec, / is ignored and the tag is simply treated as <br>.
@MichałMiszczyszyn : In XHTML, the / in <br/> is mandatory. So, leaving it there both improves readability (as it's more obvious that the tag is self-closing) and improves backward compatibility with pre-HTML5 environments.
|
0

Another try, try to add the string in a element like a p or a h1 or something

function getCoverage() {
    var selectedMarkers = selectedTowns;
    var selectedString = "";
    console.log(selectedMarkers);
    for (var key in selectedMarkers) { // run for every value in selectedMarkers
        console.log(key);
        if (selectedRegions.indexOf('ZA-' + key) > -1) { // if current province is one of the selected provinces
            if (selectedMarkers[key].length > 0) { // check if the subarray of selectedMarkers contains a value (if province has selected town)
                selectedString = selectedString + province[key] + " selected towns"; // add the province code as well as 'selected towns' to return string
            } else {
                selectedString = selectedString + province[key]; // adds only province name (since regional coverage for this province)
            }    
            selectedString = selectedString;
        }
    }
    return "<p>" + selectedString + "</p>";
}

Comments

Your Answer

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