Results 1 to 6 of 6

Thread: javascript get element by class

  1. #1
    Join Date
    Nov 2009
    Posts
    518

    javascript get element by class

    I am a student and working on a small web project which is designing with the help of HTML and java script.I have a question regrading this project and I hope,you all will help me to solve my query.My Question is that Is there a way to access an element by class instead of ID so you could assign an action to more than one element at once?

    Please,It's all depends on your suggestion.

    Thanks.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    javascript get element by class

    Hi !

    This is very nice question and I think it can be done through the class named getelementbyclass which can be suitable to access all the elements of your page.

    As a student,I will suggest you to use some different thing which are new in and being used by most of the developers.You are also an student and can perform different experiments of using different things.

    You can use Jquery for this which is newer and can help you a lot.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    javascript getelementbyclass

    Before starting anything, just take a look on some important discussion.The modern browsers have already implemented the getElementsByClassName() method.

    I am going to show you a code which will access the elements of page during loading of page .The getelementbyclass can be used in the program as follows-

    Code:
    onload=function(){
    if (document.getElementsByClassName == undefined) {
    	document.getElementsByClassName = function(className)
    	{
    		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
    		var allElements = document.getElementsByTagName("*");
    		var results = [];
    
    		var element;
    		for (var i = 0; (element = allElements[i]) != null; i++) {
    			var elementClass = element.className;
    			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
    				results.push(element);
    		}
    
    		return results;
    	}
    }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Changing image style using get element by class

    Changing image style

    According to above discussion.it has been very clear that you can access the elements of the pages using this class.So I would tell you how a image can be accessed through this function and possibility to change the behavior of image using some special arguments.

    The code to access the image from the page would be -

    Code:
    var img_array = document.getElementsByTagName("img");
    for (lp_var = 0; lp_var < arr.length; lp_var++) 
     {
       arr[lp_var].style.width = arr[lp_var].style.width ? "" : "74%" ;
     }
    The above written code will change the width of the image.

  5. #5
    Join Date
    Jan 2008
    Posts
    1,521

    getelementbyclass with javascript event

    The getelementbyclass is specified as the method in the web programming.We can attach it with different elements and also the event handler.

    I think,you all would be aware of the event programming through the javascript which uses different events like-onmouseover,onmouseout,onclick,etc.

    We can use this method with those events as a demonstration is given -

    Code:
    <a href=”…” onclick=”getElementByClass(‘whatever’);”><img src=”…”></a>
    Just include the above code with appropriate information means other arguments in the tag and refresh the browser after saving the changes.

  6. #6
    Join Date
    Apr 2008
    Posts
    1,948

    Example Get Elements By Class Name

    This one is an independent example of Get Elements By Class which is able to access the whole elements of the HTML document.

    You need to just specify the name of function and pass the BODY argument in the parenthesis of the function,The definition can take this form -


    f
    Code:
    unction getElementsByClassName(classname, node) {
       
          if(!node) node = document.getElementsByTagName("body")[0];
       
          var arr = [];
       
          var reg = new RegExp('\\b' + classname + '\\b');
       
          var Elt = node.getElementsByTagName("*");
       
          for(var lp1=0,j=elt.length; lp1<lp2;lp1++)
       
          if(reg.test(els[lp1].className))a.push(els[lp1]);
       
          return arr;
       
          }

Similar Threads

  1. How to set the value of a form element using Javascript?
    By shivendra in forum Software Development
    Replies: 4
    Last Post: 07-02-2010, 05:04 AM
  2. How an Array Class is Used in Javascript?
    By PsYcHo 1 in forum Software Development
    Replies: 5
    Last Post: 05-02-2010, 12:04 AM
  3. Code to scroll to javascript element.
    By sizzla in forum Software Development
    Replies: 2
    Last Post: 04-07-2009, 10:13 PM
  4. JavaScript - How to create Element
    By Xylina in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 05:06 PM
  5. How do I know the width of an element through javascript?
    By Conner in forum Software Development
    Replies: 3
    Last Post: 06-04-2009, 11:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,021,330.11295 seconds with 17 queries