8 Replies - 1052 Views - Last Post: 08 March 2013 - 02:39 AM

#1 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

getElementsByName?

Posted 07 March 2013 - 12:19 PM

getElementById works, but if i change it to getElementsByName, it doesn't?
this works:
<div id='test'..></div>

document.getElementById('test').innerHTML='something';


<div name='test'..></div>
<div name='test'..></div>


document.getElementsByName('test').length
is always 0
document.getElementsByName('test')[0]
is always undefined?

the div's are written out by jQuery.

This post has been edited by g37752: 07 March 2013 - 12:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: getElementsByName?

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3577
  • View blog
  • Posts: 10,442
  • Joined: 08-June 10

Re: getElementsByName?

Posted 07 March 2013 - 12:35 PM

<div>s donít have names. only form control elements (like <input>, <textarea>, <button>) have.

edit: images had, but thatís long deprecated.
Was This Post Helpful? 2
  • +
  • -

#3 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: getElementsByName?

Posted 07 March 2013 - 12:54 PM

i dont want to make the divs input's. what can i do? other than assigning each div a different id?

This post has been edited by g37752: 07 March 2013 - 12:55 PM

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3823
  • View blog
  • Posts: 13,549
  • Joined: 12-December 12

Re: getElementsByName?

Posted 07 March 2013 - 01:01 PM

View Postg37752, on 07 March 2013 - 12:54 PM, said:

i dont want to make the divs input's. what can i do? other than assigning each div a different id?

Give the divs the same class-name. getElementsByClassName works in most modern browsers. (Not in IE8 or below.)

caniuse

This post has been edited by andrewsw: 07 March 2013 - 01:00 PM

Was This Post Helpful? 2
  • +
  • -

#5 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: getElementsByName?

Posted 07 March 2013 - 01:52 PM

View Postandrewsw, on 07 March 2013 - 01:01 PM, said:

View Postg37752, on 07 March 2013 - 12:54 PM, said:

i dont want to make the divs input's. what can i do? other than assigning each div a different id?

Give the divs the same class-name. getElementsByClassName works in most modern browsers. (Not in IE8 or below.)

caniuse


yes, it does not work in IE8..
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3823
  • View blog
  • Posts: 13,549
  • Joined: 12-December 12

Re: getElementsByName?

Posted 07 March 2013 - 01:58 PM

You could implement your own version:

function GEBCN(cn){
    if(document.getElementsByClassName) // Returns NodeList here
        return document.getElementsByClassName(cn);

    cn = cn.replace(/ *$/, '');

    if(document.querySelectorAll) // Returns NodeList here
        return document.querySelectorAll((' ' + cn).replace(/ +/g, '.'));

    cn = cn.replace(/^ */, '');

    var classes = cn.split(/ +/), clength = classes.length;
    var els = document.getElementsByTagName('*'), elength = els.length;
    var results = [];
    var i, j, match;

    for(i = 0; i < elength; i++){
        match = true;
        for(j = clength; j--;)/>
            if(!RegExp(' ' + classes[j] + ' ').test(' ' + els[i].className + ' '))
                match = false;
        if(match)
            results.push(els[i]);
    }

    // Returns Array here
    return results;
}

or just use querySelectorAll('.classname').
Was This Post Helpful? 1
  • +
  • -

#7 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3823
  • View blog
  • Posts: 13,549
  • Joined: 12-December 12

Re: getElementsByName?

Posted 07 March 2013 - 02:04 PM

BTW I haven't tested that code for GEBCN (it is not mine) and there may be a better version to be found.. like this one.

This post has been edited by andrewsw: 07 March 2013 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#8 g37752  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 195
  • Joined: 24-July 12

Re: getElementsByName?

Posted 07 March 2013 - 02:36 PM

shows '[object HtmlDivElement]'? i also tried div.test

if(document.querySelectorAll('.test').length>0)
alert(document.querySelectorAll('.test')[0]);

<div class="test"..></div>

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3823
  • View blog
  • Posts: 13,549
  • Joined: 12-December 12

Re: getElementsByName?

Posted 08 March 2013 - 02:39 AM

View Postg37752, on 07 March 2013 - 02:36 PM, said:

shows '[object HtmlDivElement]'? i also tried div.test

Yes, it would do.. that is the element you are referring to?!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1