Thursday, January 29, 2009

[jQuery] Re: .val is not a function

You're returning a plain HTML Element, not a jQuery object.

Either change

return $("[id*='" + elementId + "']", $(context))[0];
to
return $("[id*='" + elementId + "']", $(context));

or change

return getElementWithContext(elementId, context).val;
to
return $( getElementWithContext(elementId, context) ).val();

- ricardo

On Jan 29, 10:26 am, trancehead <williamjhu...@gmail.com> wrote:
> Hi,
>
> I've tried it with the brackets as well. Same result.
>
> I forgot to mention that getElementWithContext  is defined as
>
> // Returns the element using JQuery to find ASP.NET control ID  -
> includes context for speed
> getElementWithContext = function(elementId, context) {
>     // Returns all elements that end with the elementId
>     return $("[id*='" + elementId + "']", $(context))[0];
>
> }
>
> This is from firebug:
> getElementWithContext(elementId, context).val is not a function
> [Break on this error] return getElementWithContext(elementId,
> context).val();
>
> William
> On Jan 29, 2:11 pm, Stephan Veigl <stephan.ve...@gmail.com> wrote:
>
> > And that's right, ".val" is NO function,
> > the function is ".val()".
>
> > by(e)
> > Stephan
>
> > 2009/1/29 trancehead <williamjhu...@gmail.com>:
>
> > > All the core functions for JQuery seem to have stopped working.
>
> > > Jquery is the first script include so it appears before the other
> > > functions.
> > > <script src="../../Shared/JS/jquery-1.3.1.min.js" type="text/
> > > javascript"></script>
> > > <script src="../../Shared/JS/Functions.js" type="text/javascript"></
> > > script>
> > > <script src="../../Shared/JS/PPFFunctions.js" type="text/javascript"></
> > > script>
>
> > > I'm using asp.net webforms so I have a function that fetches each
> > > elements value for me according to the ID
>
> > > // Returns the value of an element using JQuery to find ASP.NET
> > > control ID - includes context for speed
> > > getElementValueWithContext = function(elementId, context) {
> > >    // Returns all elements that end with the elementId
> > >    return getElementWithContext(elementId, context).val;
> > > }
>
> > > This gives the error .val is not a function. The strange thing is if I
> > > return getElementWithContext(elementId, context).value; it works fine.
>
> > > This is not really a huge issue but it affects other core functions
> > > such as .html which I use a lot.
>
> > > I've been trying to figure this out for 2 days now and I'm at an
> > > absolute loss. Any suggestions?

No comments: