0

I have a form application. I need to access an input object. I can not add this attribute.

i am getting error :

Uncaught TypeError: Property 'children' of object # is not a function scripts.js:106 (anonymous function) scripts.js:106 x.event.dispatch jquery.js:4597 y.handle

elm[6].children() = Not null

t[0].value = Have a value.

$(".demo").delegate(".configuration .dropdown-menu button", "click", function (e) {
    e.preventDefault();
    var t = $(this).parent().children();

    var elm = t.parent().parent().parent().parent().parent().children().children();
    debugger;

    if (t[1].id == "Id") {
        elm[6].children().attr('id', t[0].value);
    }
})

I can see input object details. But i cannot add attribute..

2
  • Please replace t.parent().parent().parent().parent().parent() with t.closest("selector"). Your JS is very brittle to the slightest HTML change. Also, probably what you want is t.closest("some selector").find("some other selector") so you're not hard coding specific HTML positions into your JS. Commented Mar 28, 2014 at 21:48
  • absolutely right! the first that came to mind was this way. thank you for the suggestion. i'll try. Commented Mar 28, 2014 at 21:59

1 Answer 1

3

Use eq() instead of brackets, as [6] accesses the underlying dom node in the array-like jQuery object, and then you no longer have a jQuery object that can be chained with jQuery methods etc

elm.eq(6).children().attr('id', t[0].value);

Note that the way you're doing this could potentially set the same ID to multiple elements, which is not valid.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.