1

I'm having a hard time with impromptu(jquery plugin) where I keep getting an undefined value. Here is an example:

<img id=dispatch title='Click to dispatch(COMING SOON)' onclick=Dispatch(0) src='../images/icons/truck_green.png' width=16 height=16/>
<img id=dispatch title='Click to dispatch(COMING SOON)' onclick=Dispatch(1) src='../images/icons/truck_green.png' width=16 height=16/>
<img id=dispatch title='Click to dispatch(COMING SOON)' onclick=Dispatch(2) src='../images/icons/truck_green.png' width=16 height=16/>
<img id=dispatch title='Click to dispatch(COMING SOON)' onclick=Dispatch(3) src='../images/icons/truck_green.png' width=16 height=16/>

function Dispatch(id){
           var driver = 'Enter the drivers name:<br><input type="text" id="alertName" name=myname value="" />';

           $.prompt(driver,{   
                    submit: myfunc,
                    buttons: { Ok:true }                         
            });
        }  

function myfunc(e,v,m,f){                
            var an = m.children('#alertName');

            if(f.alertName == ""){
                    an.css("border","solid #ff0000 1px");
                    return false;
            }
            return true;
        }

Eventually I want to take the id and the name typed and do some AJAX. But I cant get the css change. I alerted f.alertName and got undefined and I don't know why. Thank for your help.

LouieV
  • 1,032
  • 2
  • 15
  • 28

1 Answers1

1

I believe you want f.myName. Learn to step through with debugger and examine objects. The answer quickly became obvious.

http://jsfiddle.net/ULeQz/1/

var driver = 'Enter the drivers name:<br><input type="text" id="alertName" name="myname" value="" />';

$.prompt(driver, {
    submit: myfunc,
    buttons: {
        Ok: true
    }
});

function myfunc(e, v, m, f) {
    debugger;
    var an = m.children('#alertName');

    if (f.myname == "") {
        an.css("border", "solid #ff0000 1px");
        return false;
    }
    return true;
}​
mrtsherman
  • 39,342
  • 23
  • 87
  • 111
  • I had no idea about debugger, anyways shouldn't alertName and myname point to the same element? – LouieV Aug 28 '12 at 02:14
  • @LouieV - `myname` is a property of the object `f`. f is not an element so you are not pointing to it. The `f` object is created by the plugin. Try pressing F12 in Chrome. This brings up the debugger. By placing the line `debugger;` in the code you force a breakpoint. Then you can examine the variables and objects as well as properties on those objects. – mrtsherman Aug 28 '12 at 02:17
  • Thanks. Using myname solved it, although I don't get why in the example on the plugin page uses the id alertName. – LouieV Aug 28 '12 at 02:23
  • 1
    @LouieV - In that example the `id` and `name` are the same. In yours, you changed `name`. It never used id the example in the first place, it always used name. You just interpreted it incorrectly. – mrtsherman Aug 28 '12 at 02:28