This question already has answers here:
jQuery Data vs Attr?
(3 answers)
Closed 6 years ago.
Im having an issue or possibly a expected result with JQuery's .data() method. Im unsure why when using the element selector the data values are not updated after running the data method. See the screenshot for an example of what im talking about.
Im unsure why when using the data(key,value) method, it updates the elements data with the expected output. However why is it that it doesnt updated the values html attribute? My knowledge in Javascript is still very limited, but i'd love to know what this is and if i should expect this.
jQuery reads all data-attributes on pageload. It then deals with changes by the .data()-function only in it's storage.
To update the "real" HTML-Attribute, you need to manipulate the content like so:
$('.selected a span:eq(3)').attr('data-value', 'newValue');
Related
This question already has answers here:
How can i access javascript variables in JSP?
(3 answers)
how to access the javascript variable in jsp [duplicate]
(3 answers)
Closed 5 years ago.
I have a JavaScript variable and I want to access it in JSP without using submit button. Actually I have a javascript variable and i want to use it for running a query in JSP to fetch a value from database and again using the value from JSP to javascript.
I do not want to have a button in the form tag for submitting the value and then getting it as a paramneter in JSP. Is there any other way of doing that.
Actually i have atextbox where username is coming. I want to use that variable so that i can run a query in JSP and find its user Id. Code for getting value of text box is.
var selUserInfo = thisFrm.SelUser.value
Now i want to pass this selUserInfo to a JSP.
Not possible unless you send a GET or POST request. See this explanation.
Edit: after understanding that you're aiming for an autocompletion text-input, I would recommend to look for a jQuery solution like this one, and using an Ajax request to retrieve the data from your database (inside a .ready() event).
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 6 years ago.
I'm trying to read this link.
http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1
with this method:
$.getJSON(" http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1",function (json) {
alert(json.length);
});
But it doesn't work, and I don't know the reason :(
Some one can help me?
[edit]
I just want to read, the "json.length" its was just a test. I tried this with another .json, and worked. I want to know if a need to use some method that read without problems.
If the variable fetched is an object, instead of array, you need to use Object.keys(), which gives you the list of keys as an array and you can use the .length on it:
$.getJSON("http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1", function(json) {
alert(Object.keys(json).length);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
One thing I noticed is, the following URL:
http://wsloterias.azurewebsites.net/api/sorteio/getresultado/1
Doesn't have the CORS enabled. So, if you try accessing it, it throws an error in the Console, which is kinda silent. If you mean that, you need to request or make the JSON to have CORS Enabled.
This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 6 years ago.
First of all, I'm relatively new to this so I'm sorry if I word stuff weird.
So let's say I have the variable $x with the value 1 in PHP. I want to get the variable to my JS-Script and modify it and then send the variable x with the modified value back to my PHP document. Note I don't want to send it to a server and I don't want to use AJAX (have yet to learn it). If there's no other way It'd be nice if someone could show me an example of how to do this with AJAX.
You should use AJAX if possible it is more comfortable and with jQuery not hard to use/understand. But if this topic is new for you, you can try to send it via a window.location.href.
I looked for some good examples for you:
PHP to JS
Without AJAX
Video - JS to PHP with jQuery POST
To save PHP variables from PHP to Javascript you often just print them. If you're more familar with Javascript and PHP, you should try to work with JSON-Strings. But I think that would be to much for your first try.
Hope that helps.
This question already has answers here:
Android WebView always returns null for javascript getElementById on loadUrl
(3 answers)
Closed 5 years ago.
We have an app that uses a form for oauth, just for dev purposes I want to eliminate typing in the user name and password so I added some code like this for when the page finishes loading:
mWebView.loadUrl("javascript:document.getElementById('UserName').value='" + txtUser.getText()+"'");
But for some reason it doesn't fill in the form it makes a new page and just writes out the value of txtUser instead of filling in the input field? Why and how can I fix this?
If you look at a tool look Squirt it does an anonymous function call. So I am not that versed in javascript, but that anonymous call seems to be the key in that the browser sees it as the current page is making the call itself. So try this roughly:
mWebView.loadUrl("javascript:(function(){document.getElementById('UserName').value='" + txtUser.getText()+"';})()");
I faced a similar issue and since I couldn't find a proper solution, I used the following hack -
Fetch the html source text.
Replace </body> tag with </body><script>YOUR JS CODE</script>
webview.loadDataWithBaseURL(url, editedString, "text/html", "UTF-8", null);
Sorry for the hacky solution, but in case you can't find a solution, this could help :)
This question already has answers here:
How do I get query string value from script path?
(5 answers)
Closed 8 years ago.
I wanna know is this possible I pass get parameters for a javascript file and use them in codes?
I have this in html:
<script type="text/javascript" src="/javafile.js?q=somtext"></script>
know how I use this "q" parameter in my script codes? is this possible?
You can either:
Generate the JS dynamically with a server side language and read the query string there
Assume that the JS is the last <script> element so far in the DOM and parse document.scripts[document.scripts.length - 1].src
Note that if the script element has been added dynamically (i.e. with JS), it might not be the last script in the DOM.
I think Quentin's suggestion is the answer to your question.
I usually use an alternative way for this, which may also help you:
Make sure that your javascript is written in a library form, and make sure that you have an instantiate method/function inside your javascript that allows you to pass parameters (or better, as an object)
// on dom load:
library.init({ var1: value1, var2: value2});
This allows you also to load your javascript in a different way, and allows for cleaner code.
Or you can use option 3: use a library that has this functionality, e.g. MooTools: http://mootools.net/docs/more/Types/String.QueryString