1568 posts
  • Has been part of the Envato Community for over 6 years
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
+2 more
rahulbist says

hi everyone,

I am a newbie in jquery.

I have a form inside div tags. I am not able to access the textfield of form in jquery.

structure :

‘div id = “first”’

‘div id = “second”’

<form id= “new”>

<input type=”text” name= “gnum” id = “gnum” />

</form>

’/div’

’/div’

when the form wasn’t inside div tags. I used to access the textfield by

$(’#gnum’).val();

but, now I can’t. Can anyone tell me what will be the correct syntax for it?

please help me.

thanks in advance.

3752 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Located in Spain
  • Has sold $125,000+ on Envato Market
  • Helped several times protecting Envato Market against copyright violations
+9 more
pezflash says

Hi. I use this function to locate input fields and empty them. You can customize it up to your needs.

Function:
function initForm(oForm, element_name, init_txt) {
        frmElement = oForm.elements[element_name];
        frmElement.value = init_txt;
}
Call to function parsing form ID (first one document, in this example), input name and new text value:
initForm(document.forms[0], 'email', '');

Hope this helps! ;)

14 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $100+ on Envato Market
  • Sells items exclusively on Envato Market
Web_ON says

Hi, rahulbist

When you address an element in jquery by its id, its DOM position doesn’t really matter, because an id is a unique attribute. If you get errors with that there are reasons like: - the id of your element is not unique ///to fix that make it unique - there is no value attribute ( if you have $(’#...’).val() ) ///to fix that give your input a value - the code is operated before the DOM is formed. /// to fix that either place your script block in the end of the document, or wrap your code with $(document).ready(function () { ..... }) .

In fact I came across problems with older IE versions to behave well with $(’#...’) – addressing elements by id. So addressing it by classes or other attribute or filter can solve it. But in this case we need to remember that class is not a unique attribute. So for you case it can be like this: $(’#new’).children(‘input[type=”text”]’).val() , but even better, (if your form is the only one on the page) is $(‘form’).children(‘input[type=”text”]’).

in your example you use div with ids. I’d better give them unique classes instead. In that case I could be safe from IE6 issues.

jquery is a very flexible library, so experiment with it and good luck.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by