I'm trying to do the following thing: <div>javascript:processData(Model.Property)</div>, where "processData" is a function that returns a string. How can this be done?
(This obviously is not working, it just returns the string "javascript:processData(Model.Property)".)
You can call the javascript method on jquery document ready and set the result to that div using html method.
#model YourSomeModelWithThatProperty
<div id="thatDiv"></div>
<script type="text/javascript">
$(function(){
var thatDataString=processData("#Model.Property");
$("#thatDiv").html(thatDataString);
});
</script>
Read the following question :
Using Razor within JavaScript
You need razor text markers:
http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx
<script type="text/javascript">
function processData(property) {
// all kinds of stuff...
// return result;
}
</script>
...
<input type="button" onClick="return processData(<%: Model.Property %>); />
If you're using Razor syntax, replace <%: Model.Property %> with #Model.Property.
As for displaying the result, use the javascript to put the value somewhere on the page (such as the text of an <a> element or an alert).
Related
I am using javascript innerHTML to post some data from javascript. Please find the below code:
var innerHTML='<% SomeClass.getSomething %>';
document.getElementById("something")=innerHTML;
HTML:
<div id="something"></div>
Now i am able to see the output as <% SomeClass.getSomething %>. But how do i render it to show the value of getSomething. Please Help.
The SomeClass is defined in jsp where the html is there.
Assuming your code is in a JSP file I think you want the following
var innerHTML='<%= SomeClass.getSomething %>';
The <%= tells the JSP to output the expression in place.
Try
var innerHTML='${SomeClass.getSomething}';
If you are passing it througn model.
I have below line in my grails gsp file.
<div class="pagination">
<g:paginate total="${lotCount ?: 0}" />
</div>
I want to pass lotCount value to one of my javascript named area-switcher.js file to further use it. How can I do this?
I tried to refer one suggestion from How to pass a value directly from a controller to a javascript file in Grails
where I do below in my gsp
<g:javascript> var theId = ${lotCount} </g:javascript>
and try below in my js file for testing
alert(theId);
but it doesn't work.
Got error like ReferenceError: theId is not defined.
Please help.
Use a hiddenField:
<g:hiddenField name="lotCount" value="${lotCount}" />
<div class="pagination">
<g:paginate total="${lotCount ?: 0}" />
</div>
Your solution should work.
Just ensure that in source of generated html page line
<g:javascript> var theId = ${lotCount} </g:javascript>
is placed before line which includes area-switcher.js
<script type="text/javascript" src="area-switcher.js">
Besides that, there are two more options to pass some value from .gsp to javascript file (examples use jQuery):
By using of data attribute. For example,
gsp (here div, span, input, other tags could be used):
<div id="countElem" data-count="${count}">
js(jQuery used here):
var count = $("#countElem").data('count');
As was mentioned in another comments, by using of hidden field:
gsp:
<g:hiddenField name="countElem" data-count="${count}"/>
js(jQuery used here):
// hidden field will have name and id equals to countElem
var count = $("#countElem").val();
how to set grails variable in javascript?
I also tried
<a onclick="myFunction(${aCase.id})" .../>
...
<script>
function myFunction(id) {
<g:set var="caseId" value="id"/>
}
</script>
value of caseId always 'id' (string), i want it value of 'aCase.id'
thanks!
(sorry, i not good at english)
You can not set a gsp var with javascript. Cause the gsp var is set before the gsp is rendered on server side, and the javascript runs on client side.
<script>caseId = ${id}</script>
I would like to call the following javaScript function so it gets outputted to a HTML P tag, but am not sure how to do this without explicitly calling the function in the HTML file.
I do not want to do this...
<p class="showcode">
<script type="text/javascript">
wise_words();
</script>
</p>
I would like to keep the javaScript code all in one js file.
I have tried it this way but this does not seem to work...
document.getElementById("showcode").innerHTML = wise_words();
I would really appreciate any help as to what I am doing wrong.
Here is my code... http://codepen.io/anon/pen/qfLdE I would like to have the generated text get outputted inside the grey box.
You should call the function in an onload handler, so that it is executed after the DOM has been constructed:
window.onload = function() {
document.getElementById("showcode").innerHTML = wise_words();
}
Another problem is that your wise_words() function is using document.write (please don't use document.write) instead of returning a value. You need to return a value:
var retText = wiseText[nextVal][0];
nextVal += 1;
writeCookie("wisewords", nextVal.toString(), 33);
return retText;
Try following using Jquery:
$(".showcode").html(wise_words());
NOTE: Assuming your function returns the HTML/text.
<p class="showcode">
<script type="text/javascript">
document.write(wise_words());
</script>
</p>
or:
<body onload="document.getElementById('showcode').innerHTML = wise_words()">
<p id="showcode">
</p>
</body>
(note id instead of class).
<div id="picker-updatedDate{%= ID %}" class="bordForTdPickerTables cellPadding cellNotEditable lastUpdateDIV">{%= ConvertDateFromMSajax2JSFullDateStr(EnteredWhen) %}</div>
But the function ConvertDateFromMSajax2JSFullDateStr doesn't get called.
How can I call the function?
Assuming ConvertDateFromMSajax2JSFullDateStr is client side JavaScript function and you want to pouplate the div with its output, passing server side argument, first have this code in your page:
<script type="text/javascript">
window.onload = function() {
var oDiv = document.getElementById("picker-updatedDate<%=ID%>");
oDiv.innerHTML = ConvertDateFromMSajax2JSFullDateStr("<%=EnteredWhen%>");
}
</script>
Then correct syntax for the div itself is:
<div id="picker-updatedDate<%=ID%>" class="bordForTdPickerTables cellPadding cellNotEditable lastUpdateDIV"></div>
Make sure to have the code assigning values to ID and EnteredWhen variables before the above.
Your question is unclear and your code is syntactically incorrect. As others have pointed out, there is no javascript function provided in your example.
ASP delimiters are "<% %>" not "{% %}"
To "call" a function in ASP you can use :
<% call MyFunctionName() %>
However, by using the explicit call statement, no value is returned from the function.
if the function is to return a value then omit the call statement:
<%= MyFunctionName() %>