Jump to content

line break added within innerHTML?


Recommended Posts

Apparently, I have a line break within my innerHTML in a span tag with id=accountPanelTxt .

I have to add a line break in a variable I am testing, as shown here.


if (x == '\ntrue') {


This is the resulting HTML, before the username innerHTML is altered:

<span id="accountPanelTxt">

<span id="usernameTxt">


This is the Javascript:

alert("got here1b");
loggedIn = document.getElementById("accountPanelTxt").innerHTML;
alert("logged in: "+loggedIn);

var userName = document.getElementById("usernameTxt").innerHTML;
alert("userName: "+userName);


x = document.getElementById("accountPanelTxt").innerHTML;
alert("x: "+x);

z = 'true';
alert("z: "+z);

y ='';
menuTxt = '';

if (x == '\ntrue') {
	y = "T";
		menuTxt = '<div class="dropdown"><button class="dropbtn">'+userName+'</button><div class="dropdowncontent"><a href="#">My Account</a><a href="#">My profile</a><a href="#">Profile Settings</a><a href="#">Notifications</a><br><a href="logout.php">Logout</a></div></div>';
} else {
	y= "F";
	menuTxt = '<a href="indextemplate.php" id="loginlink">Login</a></td><td><a href="indextemplate.php" id="loginlink">Register</a>';
document.getElementById("accountPanelTxt").innerHTML = menuTxt;

alert("got here2b");


Link to comment
Share on other sites

You are inserting  invalid block elements (div), also invalid td , as it should be within a tr element. This will cause the browser to render these invalids  element correctly creating extra empty elements causing the problems  you are experiencing.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...