www.webdeveloper.com
Results 1 to 4 of 4

Thread: problem with eventhandler

  1. #1
    Join Date
    Nov 2011
    Posts
    4

    problem with eventhandler

    Hi everyone
    I have written a script that finds all td elements with "description" class name. and if their innerHTML.length be bigger than 50 characters for example it will shorten the text by substr() function to 10 characters for example. and there is a show link that if clicked it will show the full text of that element. but the problem is that when i click show link it will show the full text for a very short while and then it will be shorten to that 10 characters again.
    I am not sure where is problem and here is the code:
    Code:
    <table>
    <tr><td class = "description" id="4">some text some text some text some text...</td></tr>
    </table>
    <script type="text/javascript">
    	function getElementsByClass(theClass)
    	{	
    		var elemArray = [];
    		var matchedArray = [];
    		elemArray = document.getElementsByTagName("*");
    		var pattern = new RegExp("(^| )" + theClass + "( |$)");
    		for(i = 0; i < elemArray.length; i++)
    		{
    			if (pattern.test(elemArray[i].className))
    			{
    				matchedArray[matchedArray.length] = elemArray[i];
    			}
    		}
    		return matchedArray;
    	}	
    	x = getElementsByClass("description");
    	y = new Array();
    	for (i = 0; i < x.length; i++)
    	{
    		if(x[i].innerHTML.length > 5)
    		{
    			y[i] = x[i].innerHTML;
    			x[i].innerHTML = x[i].innerHTML.substr(0,5) + "<a href='' class='" + i +"' onclick='show(this.className);'>show</a>"; 
    		}
    		else
    		{
    			y[i] = x[i].innerHTML;
    		}
    	}
    	function show(classname)
    	{
            	x[classname].innerHTML = y[classname];	
    	}
    </script>
    Last edited by Kor; 11-21-2011 at 03:38 AM. Reason: wrap the code [code][/code]

  2. #2
    Join Date
    Jan 2009
    Location
    Insanity
    Posts
    1,131
    ANOTHER ONE NOT WRAPPING IN FORUM TAGS..!

    WHATS WRONG WITH PEOPLE, CAN THEY NOT SEE THE # <> & PHP buttons!

    besides that.

    x[i] is an object and innerHTML would return "some text some text some text some text..." and any HTML in that string.

    innerText will return the string and not the HTML

    Also you have on a cursory examination a few typos, I suggest you go back, put some indentation in to your code and watch your pairing of braces {} as you have atleast one mismatch and you need to look in to the
    Code:
    x[i].innerHTML = x[i].innerHTML.substr(0,5) + "<a href='' class='" + i +"' onclick='show(this.className);'>show</a>";
    line a little more as your use of this is wrong, it needs to be the target wrapper which in your example is called description so my thoughts are that it might need to be something like
    Code:
    x[i].innerHTML = x[i].innerHTML.substr(0,5) + "<a href='' class='" + i +"' onclick='show("+x[i].name+".className);'>show</a>";
    but without seeing your code and how it fits with what you have posted, its hard to say.

    More code please is all Ican say and WRAP in the prpper tags, # = CODE like Javascript if your posting JS only, <> for HTML and I suggest you post your full HTML wrapped in these tags so people looking in can help.
    We all have baggage to carry in life, unfortunately for me I always get the trolley with the wonky wheel...
    Code:
    Youre = {
          STILL_not_getting_it:function(){
               alert("YOU, the original poster / thread starter NEED to POST the code and NOT a LINK.");
          },
          MissingThePoint:function(msg){
                alert("You're missing the point. " + msg);
          }
    }
    Youre.STILL_not_getting_it();

  3. #3
    Join Date
    Nov 2011
    Posts
    4

    Talking thanks

    thanks JunkMale and sorry for my mistake.
    I'm going to wrap my codes in future .

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by mustafa-shujaie View Post
    thanks JunkMale and sorry for my mistake.
    I'm going to wrap my codes in future .
    You could have done that right away, by re-editing your post. Anyway, I did that for you...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles