\n or <br\> is not working in javascript - javascript

I am setting value of hiddenfield from code behind in vb.net
If(condition)
hdmodulename.Value =Dt_Module.DefaultView(0).Item("vModuleCode").ToString() +
"-" + Dt_Module.DefaultView(0).Item("vModuleName").ToString()
Else
hdmodulename.Value = hdmodulename.Value + "'\n'" + Dt_Module.DefaultView(0).Item("vModuleCode").ToString() + "-" + Dt_Module.DefaultView(0).Item("vModuleName").ToString()
End If
now alert in javascript
i code like below:
alert('You cannot select ' + document.getElementById('<%=hdmodulename.ClientID%>').value);
Output :
'\n' is printed inseted of new line
so what is the probem??
Thanks A ton in advance

As for web pages, break lines with <br> or <p></p> tags. You can also Use Environment.NewLine with VB.
For JS use \n' for the line break -
alert("some text\nmore text in a new line");

Please try with Environment.NewLine for VB.net
as below:
Dim value As String = "[First" + _
Environment.NewLine + _
"Second]"
will return
[First
Second]

Not a vb expert, but shouldn't it be "\n" instead of "'\n'"?
The one in the vb code have both double and single.

Related

JavaScript appendChild text node doesn't break line

a JavaScript n00b here...
I'm generating some html code in javascript, that is going to be displayed as code via the prism HTML markup plugin. The code is dynamically added to a <pre> tag on a button click.
My javascript code is as below. It is the text in line 2, where I need a line break. I have tried /n but that doesn't work it just makes a space.
var startLabelTag = document.createTextNode("text goes here");
startLabelTag.nodeValue = "<label><strong>" + elementNameFinal + "</strong></label>LINEBREAK HERE<select id='dropdownmenu' class='Custom_" + fieldNameFinal + "' onchange='selectChanged('#field[" + fieldNameFinal + "]',this.value);'>";
document.getElementById("dropdown-code").appendChild(startLabelTag);
Below is the text string I'm trying to create, where a line break is made where the text LINEBREAK HERE is.
<label><strong>" + elementNameFinal + "</strong></label>LINEBREAK HERE<select id='dropdownmenu' class='Custom_" + fieldNameFinal + "' onchange='selectChanged('#field[" + fieldNameFinal + "]',this.value);'>
Is it something like this you are looking for?
By using String.fromCharCode(10) you can insert a line break and with the pre tag (or div having white-space: pre-wrap) the line break will be visible/shown.
var elementNameFinal = "elementname", fieldNameFinal = "fieldname";
var startLabelTag = document.createTextNode("text goes here");
startLabelTag.nodeValue = "<label><strong>" + elementNameFinal + "</strong></label>" + String.fromCharCode(10) + "<select id='dropdownmenu' class='Custom_" + fieldNameFinal + "' onchange='selectChanged('#field[" + fieldNameFinal + "]',this.value);'>";
document.getElementById("dropdown-code").appendChild(startLabelTag);
<pre id="dropdown-code"></pre>
Side note
You can of course use a div as well, having the CSS rule Niet the Dark Absol suggested.
<div id="dropdown-code"></div>
#dropdown-code {
white-space: pre-wrap;
}
Add white-space: pre-wrap to the container's CSS.
After all, if you type a newline in your HTML source, do you get a blank line in the result? Nope. Not without making whitespace significant through CSS.

Calling function from c# to JS File

I am using an inline jquery in the c# code and it works. But when I try to call it through js file, it does not work.
C# code
tableCell.Attributes.Add("onclick",
#" if($(""div[id*='Name'] input[id*='" +
checkboxID + #"']"").is(':checked')" );
Here, I get the value true. So Now I try to make a function in js file and call it.
tableCell.Attributes.Add("onclick",
"javascript:Foo(this,'" + checkBoxID + "');
return false;");
Js File :
function ToggleScorecardConfig(sender,checkboxID) {
var a = checkboxID + "#";
alert(a); //WORKS
alert($(""div[id*='Name'] input[id*='" + checkBoxID +
#"']"").is(':checked')); //ERROR : ')' Expected
}
I know I am doing some very small mistake, Can anyone please guide me.
You have double double quotes in the JS file code, should just be:
$("div[id*='Name'] input[id*='" + checkBoxID + "']")
The '#' is used to escape the double quotes in C#, that's why the in-line code works, you don't need that in JS

javascript next line(\n) on the string not working

Hi this does not make sense I am trying to get put \n to break my sentence but it seems not working ,my code below
"Address:"+location.addressLine1 + " " + location.addressLine2 +"\n "+"Store Name:\n"+ location.storeName +" "+"Geofence:\n"+ location.maxGeofence+" "+"City:\n"+location.city
my goal is to have all the rows in new line ,Like below
Address:xxxxxxx
Store Name:xxxxxx
Gofence:xxxxxx
City:xxxxxxx
but I keep on getting
Address:xxxxxxx Store Name:xxxxxx Gofence:xxxxxx City:xxxxxxx
Either use <br/> in HTML or, if you wish to keep using \n, use the nl2br function from php.js :
function nl2br (str, is_xhtml) {
var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}
This will convert every \n to a <br/>
Instead of using \n, use <br/>
<br /> is HTML element for a line break. <br /> allows to show up as a new line when HTML is rendered in a browser.
In html \n has no effect. Replace it with <br/> or place your code inside <pre> tags.
br - will make a new line inside HTML
pre - code inside will be formated as it was inside a text file (it will understood \n), but you will have to style it.
Here is solution for newline
"Address:"+location.addressLine1 + " " + location.addressLine2 +"<br>"+"Store Name:<br>"+ location.storeName +" "+"Geofence:<br>"+ location.maxGeofence+" "+"City:<br>"+location.city
Note : \n is work only php not in Html so \n will not work here.

Add a "new line" in innerHTML

I am trying to create a table with images in first cell and information about the pic in second cell.
I need to add different information in one cell, like that:
cellTwo.innerHTML = arr_title[element] + arr_tags[element];
Is it possible to add a "new line" there?
I mean like that:
cellTwo.innerHTML = arr_title[element] + "/n" + arr_tags[element];
The simplest way is by adding a line break as html
cellTwo.innerHTML = arr_title[element] + "<br />" + arr_tags[element];
If you want your newlines to be treated literally, you could use the <pre> tag
cellTwo.innerHTML =
"<pre>" + arr_title[element] + "\n" + arr_tags[element] + "</pre>";
To round out your understanding:
Since it is html (innerHTML) it renders html and you can use any html you wish, so in this case simply add an good old fashioned <br>:
var test = document.getElementById('someElementId');
test.innerHTML = "The answer <br>to life, the universe, and everything...<br> is 42.";
If it were a string, such as in an alert box or text box etc. then /n would be correct:
alert('Never /n Forget your towel.');
Happy Coding!
- $cr1ptN!nj#
No, <br /> does not work in asp .net but you can instead write it like so
cellTwo.innerHTML = arr_title[element] + arr_tags[element]; arr_title[element] + "/n" + arr_tags[element];
Edit - alternative wrapped in code tags
cellTwo.innerHTML = arr_title[element] + arr_tags[element];
cellTwo.innerHTML += arr_title[element] + "/n" + arr_tags[element];
Semicolon ";" seems to act as line breaks
Remember the "+=" to assign multiple values to the string

how do i create a hard return or new line in javascript

I have the following code snipet:
document.getElementById('PS_FORM/SUBJECT_PROPERTY/Business_Comments').value = com_textcl + "\n" ;
document.getElementById('PS_FORM/SUBJECT_PROPERTY/Business_Comments').value = com_text;
I want each of the values to be in their own line. Is there a way to get this done??
Thanks
Assuming that element (PS_FORM/SUBJECT_PROPERTY/Business_Comments) is a textarea, then the JS should be:
document.getElementById('PS_FORM/SUBJECT_PROPERTY/Business_Comments').value=
com_textcl + "\n" + com_text;
If it's a SELECT the code would be different (update your question).
If it's a div or some other HTML element (rather than a FORM element) then the code would be more like:
document.getElementById('PS_FORM/SUBJECT_PROPERTY/Business_Comments').innerHTML=
com_textcl + "<br />\n" + com_text;

Categories

Resources