'First time variable is stored it is Undefined, every other time it works? Uses jQuery, JavaScript, Ajax
This is part of a function:
var editid;
$("div.editable").click(function(e) {
if ($currentInput == null)
return;
var css = GetCssProperties();
$(this).css(css);
editid = $(this).attr("id");
$currentInput = null;
$("label").html("");
});
I think that the problem is here. If need be, I will post the entire thing.
Basically, the first time that var editid is stored, it is an undefined value.
The variable is send to a PHP page, and I had it echo the result back on the page.
Without fail, the first time that it stores its value onclick is "undefined" and every other time it stores the appropriate value.
Here is the entire thing:
<script>
$(document).ready(function(){
var $currentInput = null;
$("#border_button, #background_color_button, #opacity_button").click(function() {
if ($currentInput == null) {
$currentInput = $(this).prev();
$("label").html($currentInput.attr("id"));
}
});
var editid;
$("div.editable").click(function(e) {
if ($currentInput == null)
return;
var css = GetCssProperties();
$(this).css(css);
editid = $(this).attr("id");
$currentInput = null;
$("label").html("");
});
function GetCssProperties() {
if ($currentInput == null) return null;
var value = $currentInput.val();
if ($currentInput.attr("id") == "background-color") {
ajaxStyle(value, 1, editid)
return {
"background-color": value
}
}
if ($currentInput.attr("id") == "border-radius") {
ajaxStyle(value, 2, editid)
return {
"border-radius": value
}
}
if ($currentInput.attr("id") == "opacity") {
ajaxStyle(value, 3, editid)
return {
"opacity": value
}
}
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
