1

Say I have this div block:

<div id="some_id" style="display: none;">
    <form action="someAction" method="post">
        <input type="hidden" name="some-name" value="some-value">
    </form>
</div>

I need to the select the form inside this div. I am able to select the div by $('#some_id') but when I try to select the form by $('#some_id').find('form') (or get or $('#some_id:form')) I get this error:

Uncaught TypeError: $(...).find is not a function

Any possible solution?

Yar
  • 7,020
  • 11
  • 49
  • 69
  • 2
    `$` is possibly not jQuery? `console.log($.fn.jquery)` and see if you get a version number. – Taplar May 11 '18 at 20:07
  • I got `Cannot read property 'jquery' of undefined` but how come `$('#some_id')` works? – Yar May 11 '18 at 20:10
  • Possible duplicate of [What is the dollar sign in Javascript, if not jQuery](https://stackoverflow.com/questions/22244823/what-is-the-dollar-sign-in-javascript-if-not-jquery) – Taplar May 11 '18 at 20:22
  • The comment is a "Possible duplicate" not the question itself. – Yar May 11 '18 at 20:24
  • 1
    The duplicate may explain why `$` is not jQuery in your use case. `$('#some_id').find('form')` is a valid operation. If '$' is not jQuery, the answer to this question is as simple as "include jQuery in your page" – Taplar May 11 '18 at 20:26

2 Answers2

1

There is nothing wrong with what you attempted, provided that jQuery is properly included in your page and you are properly referencing it with $ or jQuery if it is in noConflict mode.

console.log(
  $('#some_id').find('form').get()
);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="some_id" style="display: none;">
    <form action="someAction" method="post">
        <input type="hidden" name="some-name" value="some-value">
    </form>
</div>
Taplar
  • 24,788
  • 4
  • 22
  • 35
0

You don't want a : to select the child you want >

Your selector should look like this:

$('#some_id > form')

Also make sure you are including jQuery, it looks like your code cannot find it.

Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338