0

I have a form seems like this:

<form id="form1">
   <div id="teste1">
       aaa: <input id="aaa" name="aaa" type="text" />
       bbb: <input id="bbb" name="aaa" type="text" />
       ccc: <input id="ccc" name="aaa" type="text" />
   </div>
   <div id="teste2">
       ddd: <input id="ddd" name="aaa" type="text" />
   </div>
</form>

So, in jquery I took the 'form1' element and put it into a variable and passed it into a function parameter:

$.function({
   var temp = $("#form1");
   foo(temp);
});

And then, the function foo is inside other script and I need find the id 'ddd', for example. How can I do it?

foo = function(dataform)
{
   dataform.$("#ddd").val() ?????
};

thanks a lot.

Domenic
  • 110,262
  • 41
  • 219
  • 271
Zanoldor
  • 15
  • 5

3 Answers3

0

have you been trying element.find(); or element.children();

sample:

foo = function(dataform, inside)
{
   var element_you_want = dataform.children(inside);
};


foo($("#form1"), "#ddd");
lngs
  • 690
  • 1
  • 8
  • 17
0

Firstly, $.function is not part of the jQuery API and will be undefined. I suspect you want to use the ready event which is typically done like this:

$(document).ready(function ({
    var temp = $("#form1");
    foo(temp);
});

Secondly, $.$ is also not part of the jQuery API and so dataform.$("#ddd") will be also undefined. Since you're trying to find a descendant element (and not a direct child element), I would recommend the find method:

foo = function(dataform) {
   dataform.find("#ddd").val() //?????
};

Also, as an aside, in the above code you are not doing anything with the value of #ddd so there won't be any discernible actions visible on the page when it successfully executes.

Finally, assuming your markup is valid and each id attribute is unique, using .find to find an element by id is less efficient than simply searching by id (i.e., $('#ddd').val()). Mind you, this obviously defeats the purpose of passing $("#form1") as a function parameter but it's more efficient (in terms of querying the DOM) and it makes me wonder what you are trying to accomplish. If you can update your question with the overall goal, we may be able to better assist you.

Community
  • 1
  • 1
pete
  • 24,141
  • 4
  • 37
  • 51
0

Working jsFiddle Demo

There is no need to define many fynctions. Add a button to your page:

<input type="button" id="check" value="Check Values" />

And attach an event (for example click) to it:

$('#check').on('click', function () {
    // do stuff
});

Now, you can get the other elements and check their value. Also you need to wait for document ready (to ensure your elements are loaded in browser):

$(function () {
    $('#check').on('click', function () {
        var ddd = $('#ddd').val();

        alert(ddd);
    });     
});