how to set attribute using javascript variable - javascript

the following is my cshtml code inside durandal application
<script type="text/javascript" src="~/Scripts/require.js"
id="countryscript" data-main="**here i have to set value**"></script>
I want to set script attribute data-main with my javascript variable value. How to achieve this ?
I tried as
document.getElementById("countryscript").data-main = countrycode;
its showing syntax error near = sign. Need help..

Try this:
document.getElementById("countryscript").setAttribute("data-main", countrycode);

Taken from MDN
var d = document.getElementById("countryscript");
d.setAttribute("data-main", countrycod);
Or if you have JQuery, this is much easier
$('#countryscript').attr("data-main", countrycod);

Related

how to console.log html tag attribute's value with hyphen in the middle in JavaScript?

I would need some help about this attribute:
<script id="mysdk" src="https://www.myscript.com/sdk/js?currency=EUR" this-client-token="nsiYXV0aG9yaXphdGlvbkZpbmdlcnByaW50IjoiZGUyYjM4N2FiZWV"></script>
What I'm trying to do is to console.log this-client-token in this way:
var el = document.getElementById("mysdk");
console.log(el.this-client-token);
This because, after making this work, I will be finally able to change the value of this-client-token, since that is my purpose. But I get the following error in the console:
Uncaught ReferenceError: client is not defined
I have no idea why I get this error, is it because of the hyphen? any suggestion?
Thank you!
Elliot
It doesn't work because - is subtraction. It's trying to calculate el.this - client - token, which fails because none of these variables exist.
Change to a data-XXX attribute, and use the dataset property.
var el = document.getElementById("mysdk");
console.log(el.dataset.clientToken);
<script id="mysdk" src="https://www.myscript.com/sdk/js?currency=EUR" data-client-token="nsiYXV0aG9yaXphdGlvbkZpbmdlcnByaW50IjoiZGUyYjM4N2FiZWV"></script>
If you can't change the attribute (because it's required by the SDK script, which you can't change) you can use el.getAttribute("this-client-token")

Javascript : how to get value from a Script Tag

I am trying to get the following value from the script below
Use this field to store the Account to which the Opportunity is related
<script>sfdcPage.setHelp('Opportunity.Account', 'Use this field to store the Account to which the Opportunity is related');</script>
i can get to this script tag by using :
document.getElementsByTagName('tbody')[6].getElementsByTagName('script')[1];
Can you please let me know.
Many thanks.
I would not recommend putting script tags within your tables, etc. In other words, I wouldn't mix script tags with other content-related HTML like that. If you want to store little bits of info like that in your HTML for the purpose of JS, I'd use data attributes instead.
The simple answer to your question though is to just use the innerHTML or innerText property as follows:
document.getElementsByTagName('tbody')[6].getElementsByTagName('script')[1].innerHTML;
Use innerHtml as
document.getElementsByTagName('tbody')[6].
getElementsByTagName('script')[1].
innerHtml;
OR
Set an id to get script content as below example
<script id='script' type='text/javascript'>
$('div').html($('#script').html());
console.log($('script'));
</script>
fiddle
There are lots of ways to do this. One solution....
var scrs = document.getElementsByTagName('script');
var newscr = "";
for (var i=0; i<scrs.length; i++) {
newscr = scrs[i].innerHtml.replace('sfdcPage.setHelp(', 'myfunction(');
if (newscr != scrs[i].innerHtml) {
eval(newscr);
}
}
(although parsing / rewriting the javascript is an ugly hack - if it's your code then your reading then you should know what you put in there).

Passing variable from JSP to Javascript

I know there is already questions about that, but I just canĀ“t simply get this work, I have a JSP file with a java variable in it:
String test = "Hello";
And I need to read this value in the Javascript embedded in the same JSP file, I tried so many options, but it is not working, and for security I don't want pass the value using the URL or hidden values.
Any ideas of how get this working?
The best way to do it is to use something like following in your javascript code;
var myvar = '${jspvar}';
I know this one is old, but this worked for me:
var javaScriptVar="<%out.print(javaString);%>";
you need to make sure that if you are using an external js file (out of the jsp file), the above line has to be before the "include" script tag. for example this is the jsp file:
var javaScriptVar="<%out.print(javaString);%>";
<script src="some_location/test.js"></script>
You can pass the value by calling some methods in html part..
<input type="submit" value="view" onclick="callpro('<%= varname %>')" />
var jsvariable="<%=test%>";
One thing to note is that you could namespace those variables in this way:
var MYAPP.javaScriptVar="<%out.print(javaString);%>";
The technique is from "Javascript: The Good Parts" Book.

Trying to access a <c:set variable in javascript

I have this line in the header of my JSP <script>var logicalName = "${logicalName}"</script> which is resolving to the correct value.
I want to access this value via javascript/jquery. How do I do this?
This is not working console.log($logicalName);
You just need to remove the string symbol from your parameter
console.log(logicalName);
console.log("${logicalName}");
and
console.log(logicalName);
Both will work!

Hyphen in div id causing javascript error

I'm having an issue with javascript whereby i am performing the following to close a popup window and update a field in the parent window with the required value. Code looks something like this:
<script language="javascript" type="text/javascript">
var FieldID = document.form.field22-1.value;
self.parent.opener.document.+FieldID = 'some text';
window.top.window.close();
</script>
However I am getting the following error:
Error: missing ; before statement
I have a funny feeling the javascript is interpreting the field id (field22-1) as having a subtraction in it. Which I guess would make sense. Any ideas/help would be ridiculously appreciated, really don't want to have to go back in and change the - in the code!
Thanks in advance!
Use document.getElementById('field22-1').value instead.
You might also need to fix this:
self.parent.opener.document[FieldID] = 'some text';
In JavaScript, any property of any object can be accessed either via dot notation, e.g. foo.bar, or bracket notation, e.g. foo["bar"]. The latter is necessary when your property is not a legal identifier (as in your case):
var FieldID = document.form["field22-1"].value;
Alternatively, if this is an actual id attribute, you should use:
var FieldID = document.getElementById('field22-1').value;
You could also use document.form['field22-1'].value.
You can use document.getElementById('field22-1').value

Categories