...

View Full Version : Can't figure out what I'm doing wrong!! Very simple.



joepinion
06-27-2012, 05:00 AM
Alright, I've oversimplified my code here to nail down what I'm doing wrong... Basically I'm having problems having a simple member function understand an object instance variable... Keep getting "undefined" in the alert. Pleas help!


var testdef;

testdef = (function() {

function testdef(davariable) {
this.davariable = davariable;
$('#dadiv').click( function() {
alert(this.davariable);
}
);
}

return testdef;

})();

And I initiate the object with this:

$(document).ready(function() {
var testobj = new testdef('hiya');
});

But the alert I get when I click my link always says undefined. I swear I have other code that looks just like this which works. Argh! Thanks for any insight you all have, I'm sure it must be some stupid typo.

DaveyErwin
06-27-2012, 03:14 PM
<!doctype html>
<head><title></title>
</head><body>
<div id="dadiv">xx</div>
<script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function testdef(davariable){
$('#dadiv').click( function cliked(){
alert(davariable);
});
}
testdef("hiyas");
</script>
</body></html>

joepinion
06-27-2012, 03:53 PM
Ok that works, how about this?


<!doctype html>
<head><title></title>
</head><body>
<div id="dadiv">xx</div>
<script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
testdef = (function() {

function testdef(davariable)
{
this.davariable = davariable;
$('#dadiv').click(this.deleteit);
}

testdef.prototype.deleteit = function() {
alert(this.davariable);
};


return testdef;

})();

daobj = new testdef('yoyo');
</script>
</body></html>

I need this to work in an object ultimately... The "deleteit" function will call a jquery get and will provide a callback function that's part of the object... so that it can keep track of the same "davariable" function, and it needs to be an object because there will be many of these running per page... So why isn't THIS code sample working?

DaveyErwin
06-27-2012, 05:48 PM
<!doctype html>
<head><title></title>
</head><body>
<div id="dadiv">xx</div>
<script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
<!doctype html>
<head><title></title>
</head><body>
<div id="dadiv">xx</div>
<script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>

function Testdef(davariable) {
function deleteit() {
alert(davariable);
};
$('#dadiv').click(deleteit);
}
daobj = new Testdef('yoyo');
daobj1= new Testdef('yoyo1');
</script>
</body></html>


Or maybe ....


<!doctype html>
<head><title></title>
</head><body>
<div id="dadiv">xx</div>
<script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
<!doctype html>
<head><title></title>
</head><body>
<div id="dadiv">xx</div>
<script id="jqueryui" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script>
function Testdef(davariable){$('#dadiv').click(davariable());}
daobj = new Testdef(function(){alert("yoyo")});
daobj1= new Testdef(function(){alert("yoyo1")});
</script>
</body></html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum