javascript - Set the name of a Jquery selector as a string in a variable -
i'm quite noob @ jquery i'm trying set variable based on name of form element.
i.e. variable = "someform" based on example:
<form id="hello" name="someform" > first name: <input type="text" name="firstname" /> last name: <input type="text" name="lastname" /> email: <input type="text" name="email" /> <input type="submit" value="subscribe" /> </form>
i'm using following code doesn't seem work:
var formname = 0; $('input').parentsuntil('form', this).focus(function() {var formname = this.name;}) if (sttime == 0) { // ensures cannot start form multiple times var sttime = new date(); stdelay = sttime - lotime; alert(formname); }
thanks in advance!
the focus
event not bubble, see http://www.quirksmode.org/dom/events/blurfocus.html#t06
couple of other issues:
- the
formname
variable being assigned inside event handler isn't same variable first line since you're re-declared it, exists inside scope of event function. parentsuntil
return ancestors untilform
element, not element presumably want.
your code out of context it's difficult understand how formname
, timer variables should used , should declared, should work event:
$('form :input').focus(function() { formname = $(this).parents('form').attr('name'); });
:input
jquery selector matches <input>
types, along <textarea>
, <select>
Comments
Post a Comment