passing dynamic parameters in script.aculo.us - javascript

I want to pass dynamic values inside parameters of script.aculo.us for auto suggestion in my jsp page
Below is my code where i want to get the value of checkbox and pass it to server. But in server side jsp its printing as it is ( i.e instead pf checkbox value its printing document.getElementById("mgmtsystem").checked )
<div class="auto_complete" id="object_name_auto_complete"></div>
<script type="text/javascript">
new Ajax.Autocompleter('<%=name%>', 'object_name_auto_complete', '<%=request.getContextPath()%>/component/ajax_introdata', { parameters: 'suggEnable= document.getElementById("mgmtsystem").checked' })
</script>
In above code i'm trying to send the value of document.getElementById("mgmtsystem").checked but its just passing it as same instead its value

If you copied your javascript into the question as it is on your code - this is why
This code does not let document.getElementById(....) get evaluated and treats it as a string
{ parameters: 'suggEnable= document.getElementById("mgmtsystem").checked' }
Try this
{ parameters: 'suggEnable='+document.getElementById("mgmtsystem").checked }
Or better yet use the utility methods built in to PrototypeJS
{ parameters: 'suggEnable='+$("mgmtsystem").checked }

Related

Pass to dwr function string stored in html input

I'm trying to pass to my dwr function the content of an html input of my page. The content of this input is changed programmatically by the user.
If i pass to my DWR method an hard coded string, the method succedes. The problem is when i give the method the content of the input. DWR just retrieves error.
The DWR function is:
function myDWRFunction(str) {
readyProxyJS.javaImplementatedFunction(str, function (data) {
$('#profileImage').attr('src', 'data:image/jpg;base64,'.concat(data));
});}
And this is the content of my jsp page (in other words, from where i'm calling the dwr method):
<input type="text" name="dynamicContent"/>
<input id="bula" type="button" value="READY" onclick="myDWRFunction(document.getElementsByName('dynamicContent'[0].value;)"/>
i'm not going to post the rest of the DWR implementation since, as i've written before, if i pass an hard-coded string to the method (example: onclick="myDWRFunction('thisIsMyHardCodedString')) , all works fine.
Can anyone help?

How can I to transfer variable from javascript to C#?

I have following code
function insertDataToSurface(i) {
$("[name=[" + i + "].Height").val('#Model.Sides.SingleOrDefault(a => a.Name == "А").Surfaces.ElementAt(i))');
}
error
Cannot resolve symbol "i"
You are defining a JavaScript function that takes a parameter i then you are trying to use it within C# code #Model.Sides.SingleOrDefault(a => a.Name == "А").Surfaces.ElementAt(i)). For that latter piece of code to be valid, i must exist on the server, not as a parameter to the client function.
Assuming the selection does need to take place on the client, you have a couple of basic options:
Send all the Surfaces across to the client and use i within the function to select the required element of that data,
Have the JavaScript call back to the server requesting a partial view containing the data, supplying i as the parameter to the URL.

Different JSON pages

i have 2 web pages containing same JSON string. one work fine with java-script function.but other one is completely not working.the only different between these two is url.
here is the page that is working fine with the js function.
(http://jsfiddle.net/echo/jsonp/?test=some+html+content&callback=?)
here is the one that is not working with the js function.
(http://sanjeewa88.byethost31.com/EMPStore/test_json.html)
here is my java-script function
function doJSON() {
$.getJSON('http://jsfiddle.net/echo/jsonp/?test=some+html+content&callback=?', function (data) {
$('#allemployees').append("<li>" + data.test+ "</li>");
});
}
what's wrong with second page and how i fix it to access that page through js function.
PS-
i want to display these data in a listview. first one is displaying that remote data on the listview.but second one which having same json string is not displaying anything.
On the page you provide the json you have to accept a para callback and use this parameter to generate the function name.
look what's happen when you call jsfiddle callback with an other name:
http://jsfiddle.net/echo/jsonp/?test=some+html+content&callback=poney
give:
poney({"test": "some html content"});
You have more information here: jQueryDoc
If the URL includes the string "callback=?" (or similar, as defined by the server-side API), the request is treated as JSONP instead. See the discussion of the jsonp data type in $.ajax() for more details.

Get guid values with js and razor

I have an Asp.Net mvc 3 project
I'm using razor, and need to generate guids in javascript
I was trying this:
<script type="text/javascript">
$(document).ready(function () {
function getNewGuid() {
return '#Guid.NewGuid()';
}
I'm using inside the click event for a button, but the second call to the function is returning the same value
What should I do for reevaluating the function with each call?
The
#Guid.NewGuid()
is evaluated server-side when the page is rendered, so you will always get the same value.
You need a Javascript Guid library from somewhere.
Try the accepted answer to this question.
While you could make an Ajax call to the server, it's pretty pointless if all you're after is a unique value that could be generated far more efficiently client-side.
Create a controller then use JSON to return a new Guid from the server. Then use $().ajax to get the value.
If you don't want to ask the server for one use the following answer
Create GUID / UUID in JavaScript?

Accessing JSON values with a variable

I'm trying to access JSON data with jQuery and grab a specific set of values based on a variable. I've done this before using [] but for some reason I can't figure out what is going wrong this time.
My JSON file (being read in by getJSON, and named jsonmaker.php) looks like this:
{"0107001":{"label":"Canada","x":"0","y":"0.34"},"0107002":{"label":"USA","x":"-0.16","y":"0.53"}}
I then have a function which is essentially this:
function addAttrib(attrib) {
$.getJSON("jsonmaker.php", function(data) {
alert(data[attrib].label);
}
}
But it keeps returning undefined. Any idea what I'm doing wrong? I've checked to make sure the var going to attrib is 0107001, no problems there.
Also, I know my JSON file is a php file so I could filter what's returned to match the attrib value, but I'm looking to develop something that can run purely on HTML and JS, so I could just pack the JSON file for the project and take it with me. No need for a web server w/ PHP etc.
The data access itself works for me:
var data = {"0107001":{"label":"Canada","x":"0","y":"0.34"},"0107002":{"label":"USA","x":"-0.16","y":"0.53"}};
var attrib = "0107002";
alert(data[attrib].label); // USA
Make sure that attrib remains untouched between the moment you call addAttrib() and the moment when the AJAX request completes and your anonymous callback function gets called.
Update: is this your real code? You have at least one syntax error:
function addAttrib(attrib) {
$.getJSON("jsonmaker.php", function(data) {
alert(data[attrib].label);
}); // <- Please note missing ");"
}
In my experience, $.getJSON() doesn't always return an object. Depending on the MIME type that the server returns along with the JSON, you might end up with a string instead of an object. Check what data contains. If it's a string, you must manually parse it using eval() (old style) or JSON.parse() (new browsers only).
try to list all properties from data, to have sure the data is being returned:
for (var p in data){
if (data.hasOwnProperty(p){
alert(data[p]);
}
}
It's not your solution but with this you can know how your data is coming.

Categories

Resources