How to escape single quote in javascript which is in jsp? - javascript

Hi I am getting property form properties file using locale,the below is the code for that
<fmt:setBundle basename="/filename" var="mybundle" />
by using the below code i am accessing one property
<fmt:message key="ErrorMesg" bundle="${mybundle}" var="ErrorMsgInfo" />
Now i want to populate this error message in javascript by using the below code which is inside of javascript
var InfoErrorMsg = "<c:out value='${ErrorMsgInfo}'/>";
I am using InfoErrorMsg inside alert.Expected behaviour :Please select 'We have a radiobutton i am getting as :Please select `&#039We have a radiobuttonby looking this it's not able to display/process single quote. To overcome this i tried the following waysFirst
String result = (String)pageContext.getAttribute("ErrorMsgInfo");
result = result.replaceFirst("'","\\\\'");
pageContext.setAttribute("d2dFexExInfoErrorMsg1",result);
Second
var InfoErrorMsg = "<c:out value='${fn:escapeXml(ErrorMsgInfo}'/>";
Third inside javascript i replace ' with \'
InfoErrorMsg = d2dFexExInfoErrorMsg1.replace(/'/g, "\'");
and the below one also
InfoErrorMsg = d2dFexExInfoErrorMsg1.replace(/'/g, "\\\\'");
Can anybody please help me to solve this one

Related

Multiple attribute on href using onclick

I Tried this code to get multiple value in href but it does not work. any problem on this one ?
Print
You are missing a + sign between a string and a value.
The error is between this two
document.getElementById('CUS_CODE_MX').value '&AGE='
Correct format
document.getElementById('CUS_CODE_MX').value + '&AGE='
Every time you join a value and a string, you need a + sign
Even if you are joining two strings
'Hello'+ 'World'
Pliss avoid long js as an inline atribute. I will recommend you call a function as the onclick attribute.
Hope this helps :)
Print
It's better to use external script for that rather than inline format. And just add missing + to your code. Also, using variables would clean up the code.
function func() {
var CUS_CODE_MX = document.getElementById('CUS_CODE_MX').value;
var AGEID = document.getElementById('AGEID').value;
this.href = 'printsales.php?CUSTOMERID='+CUS_CODE_MX+'&AGE='+AGEID;
}
Print

Add child to E4X with my variable

This is using Mirth Connect which uses E4x and js.
Basically I have a variable that I want to populate the XML with.
var memberid = "1234";
var fieldsxml = new XML(<fieldvaluelist></fieldvaluelist>);
fieldsxml.field += <fieldvalue templatefieldid="446" value=#memberid/> //memberID
But its giving an error on the 3rd line: (I also tried just memberid without quotes)
DETAILS: TypeError: Open quote is expected for attribute "value"
associated with an element type "fieldvalue".
It works if the third line is this:
fieldsxml.field += <fieldvalue templatefieldid="446" value="memberid"/>
But that just adds the literal string "memberid" . I actually want value="1234" instead.
How can I do this?
Edit: The final XML should look like this.
<fieldvaluelist><fieldvalue templatefieldid="446" value="1234"/></fieldvaluelist>
You're almost there. Instead of using #memberId, use {memberId}:
fieldsxml.field += <fieldvalue templatefieldid="446" value={memberid}/>;

javascript get element id from json

I have a (simple, I guess) problem with quotes, single quotes, double quotes.
I have a JS that sends data to a php file, which responds sending some data back with json. In the code below, row.Dispon is part of the response (and is working OK). But I want to "echo" row.Element inside getElementById with no success. I've tried "+row.Element+", or "'+row.Element+'". What I'm doing wrong?
if (row.Dispon=="ImageReload") {
var text='Image changed';
document.getElementById(+row.Element+).value="due";
}
Considering your code snippet only, this should do the job for that specific problem:
if (row.Dispon == "ImageReload") {
var text = 'Image changed';
document.getElementById(row.Element).value = "due";
}
You would need quotes (or double quotes) and + operators if you were trying to build a string. See this example:
var id = 42;
document.getElementById('myId' + id).value = 'something';
Assuming that row.Element contains a string already, you can directly pass it to getElementById().
Some advice here:
Read more about functions on MDN
Read more about Document.getElementById on MDN
Consider using Document.querySelector

Viewdata in jquery syntax

I am working on an asp.net MVC application. I want to access Viewdata and assign it to Javascript variable
var LeaveDays = #(Html.Raw(ViewData["LeaveDays"]));
but it says syntax error at semi colon ; What is syntax. If i remove semi colon, it says statement not terminated. In both cases because of Js error, other scripts are not run on button click or page refresh
I want to assign the value to var
Please suggest.
Pass you value in the quotes.
Use
var LeaveDays = '#(Html.Raw(ViewData["LeaveDays"]))';
try this....
if want to assign to var :
# { var LeaveDays = ViewData["LeaveDays"]; }
or to display :
#Html.Raw(ViewData["LeaveDays"])

SYNTAX HELP: How to Find control in javascript ?

I am trying to implement server side code on client side. "trvddl1" is a ascx control which contains a dropdownlist "ddltree". It was easy on server side but I am facing difficulty using the same in javascript.
How do I write the following code in javascript?
((DropDownList)trvddl1.FindControl("ddltree")).SelectedValue;
I tried
var abc = document.getElementById('<%=trvddl1.ClientID%>').value;
and
var Region = document.getElementById('<%=trvddl1.FindControl("ddltree")%>').value;
but javascript returned error. Is there some other keyword I am missing ?
Check the HTML output (Browser-->View Source) and locate the control there, see what the ID of that control has, and put that one into the getElementById() function.
Example:
<input id='ddltree' .... />
Then use:
var abc = document.getElementById('ddltree').value;
Perhaps you can try something like that:
// find all controls that have an id that ends with ddltree
// starts with would be [id*=ddltree]
var abc = document.querySelectorAll("[id$=ddltree]");
if(abc.length > 0) {
// got it !
console.log(abc[0].value);
}
Please note that querySelectorAll is not supported in all browsers (even though - most). Here is a reference.

Categories

Resources