ricmetal Posted March 16, 2013 Share Posted March 16, 2013 hi guys so, i thought i could do something with JavaScript like jQuery does: var someElement = $('#element'); and then do: someElement.val('some data'); this would target $('#element') is this possible to do with plain JavaScript? var someElement = someFunction.functionVariable; i want to add data to the function variable through the assgined variable someElement. how do i do this? can it be done? thanks Quote Link to comment https://forums.phpfreaks.com/topic/275714-cant-javascript-trip-like-jquery/ Share on other sites More sharing options...
kicken Posted March 16, 2013 Share Posted March 16, 2013 var someElement = document.getElementById('element'); someElement.value = 'some data';That is the vanilla JS equivilent (excluding error checking) of the jQuery code var someElement = $('#element'); someElement.val('some data'); Quote Link to comment https://forums.phpfreaks.com/topic/275714-cant-javascript-trip-like-jquery/#findComment-1418962 Share on other sites More sharing options...
Adam Posted March 18, 2013 Share Posted March 18, 2013 Also, you do realise jQuery is JavaScript right? jQuery is a JavaScript library. Quote Link to comment https://forums.phpfreaks.com/topic/275714-cant-javascript-trip-like-jquery/#findComment-1419290 Share on other sites More sharing options...
ricmetal Posted March 18, 2013 Author Share Posted March 18, 2013 (edited) i know jQuery is javascript. maybe i explained myself wrong. i want to reference a function variable the same way i referenece an element with jQuery (or JavaScript using the getElementById method) as follows: var john = someFunction.someVar; and have john be a reference to the someVar value i just wanna do this to be easier to reference the var inside the function regards Edited March 18, 2013 by ricmetal Quote Link to comment https://forums.phpfreaks.com/topic/275714-cant-javascript-trip-like-jquery/#findComment-1419318 Share on other sites More sharing options...
Solution Adam Posted March 18, 2013 Solution Share Posted March 18, 2013 You can't access the variables within a function like that. Going with your example, someFunction in JavaScript represents a callable function. You can call it as many times as you like, but it has no state -- no "context". The variables defined within it are essentially lost after each call, except from what you return. It is possible for functions to retain state after they've been called, in a sense, but you need to create a new instance of the function and store variables within the this context object. Plus the function itself never has state, it returns an instance of itself which actually contains the state. For example: function foo() { this.bar = 'baz'; } var instance = new foo(); console.log(instance.bar); // outputs: bazAs you can see, new foo() returns an instance of itself we store in the instance variable, which we're then able to access the properties of. Quote Link to comment https://forums.phpfreaks.com/topic/275714-cant-javascript-trip-like-jquery/#findComment-1419341 Share on other sites More sharing options...
ricmetal Posted March 18, 2013 Author Share Posted March 18, 2013 ok it doesnt seem like a easier way than just calling the variable from in the function but thanks for the info regards Quote Link to comment https://forums.phpfreaks.com/topic/275714-cant-javascript-trip-like-jquery/#findComment-1419355 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.