Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS Javascript – Assign attribute

  • This topic is empty.
Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #31144
    bzmillerboy
    Participant

    I was able to do this with jQuery but I’m wondering how I can do the following with strictly javascript.


    Basically I want to add the attribute size=”2″ to the select element under the div with ID=”planSelectionDiv”.

    I appreciate any help.

    #67270
    bzmillerboy
    Participant

    Anyone…Anyone…?

    #67272
    TT_Mark
    Member

    Something like?

    var eSelect = document.getElementsById('planSelectionDiv ').getElementsByTagName('select');

    eSelect.setAttribute("size", "2");

    Not tested, just guessing!

    #67207
    noahgelman
    Participant

    You shouldn’t have to set a variable if you don’t want to. Depending on what you’re using it for, it probably is a better idea to. But just in case, here it is on one line.

    document.getElementsById('planSelectionDiv ').getElementsByTagName('select').setAttribute("size", "2");

    ////////
    Edit: Sorry, bad advice. After trying it out, apparently you can’t just chain them together like that. You have to use a variable like TT_Mark suggested.

    #67208
    TT_Mark
    Member

    @noahgelman As you have said, I *think* you can only chain like that in JQuery

    #67212
    noahgelman
    Participant

    Yeah, jQuery is pretty convenient for chaining things together.

    A quick question though. if bzmillerboy simply wanted to set the attribute to 2 why go:

    .attr("size", function() { return '2'; })

    and simply not:

    .attr("size", "2"})
    #67069
    TT_Mark
    Member

    No idea :S, although you have a typo in your second example

    #67012
    noahgelman
    Participant

    Oops, so I do. My bad. But still, that function he wrote confuses me

Viewing 8 posts - 1 through 8 (of 8 total)
  • The forum ‘CSS’ is closed to new topics and replies.