Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-20-2013, 02:38 AM   PM User | #1
matafy
New Coder

 
Join Date: Apr 2009
Posts: 59
Thanks: 13
Thanked 0 Times in 0 Posts
matafy is an unknown quantity at this point
Question [Help] - with innerHTML method...

I'm trying to create a function that would return as JavaScript's innerHTML.

PHP Code:
function html_(htmlContent){
    return 
this.innerHTML htmlContent;

And this is how I'm trying to call it.

PHP Code:
document.getElementById('id').html_("some html content"); 
I get this error:


Uncaught TypeError: Object #<HTMLDivElement> has no method 'html_'
(anonymous function)


I know this works...

PHP Code:
document.getElementById('id').innerHTML "some html content"
But I'm trying to shorten it.

Thanks in advance,
Matafy

Last edited by matafy; 01-20-2013 at 02:43 AM..
matafy is offline   Reply With Quote
Old 01-20-2013, 03:40 AM   PM User | #2
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 976
Thanks: 0
Thanked 203 Times in 198 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Code:
function html_(  id, htmlContent )
{
  document.getElementById( id ).innerHTML = htmlContent;
}
Logic Ali is online now   Reply With Quote
Old 01-20-2013, 03:43 AM   PM User | #3
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Arrow

Probably easier to shorten by adding one function that can be reused many times.

Code:
function $_(IDS) { return document.getElementById(IDS); }
Then you would be able to change the innerHTML property of an element like this.
Code:
<div id="divBox1">Original content</div>
<div id="divBox2">Original display</div>

...

$_('divBox1').innerHTML = 'some html content replacing original display';
$_('divBox2').innerHTML += 'some different html content added to existing display';
jmrker is offline   Reply With Quote
Old 01-20-2013, 04:00 AM   PM User | #4
matafy
New Coder

 
Join Date: Apr 2009
Posts: 59
Thanks: 13
Thanked 0 Times in 0 Posts
matafy is an unknown quantity at this point
Thanks for the response....

I made the docment.getElementByID reusable...

PHP Code:
function $_(id){
   return 
document.getElementById(id);
}
function 
html_(c){
   return 
this.innerHTML c;

I called it...

PHP Code:
$_('divBox1').html_("some html content"); 
Still get the error message. I'm trying to stay away from jQuery

I understand that I can use it like Logic Ali mentioned...

PHP Code:
html_($_('divBox1'), "some html content"); 
But I would like to concatenate it.

Last edited by matafy; 01-20-2013 at 04:09 AM..
matafy is offline   Reply With Quote
Old 01-20-2013, 07:10 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,565
Thanks: 62
Thanked 4,057 Times in 4,026 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You can *NOT* just ARBITRARILY apply a method to a DOM object. Or to any object, for that matter.

*IF* the object in question was a true JavaScript object, you could alter its prototype to add the method. But because it is a DOM object, and because the DOM is *ACTUALLY* implemented in native code under the covers, not at all in JavaScript, there is no way to directly add methods to DOM objects.

You *could* create a JavaScript "wrapper" object for your DOM objects. You might, for example, create one wrapper for all the DOM objects that indeed have an innerHTML property (you'd surely want a different wrapper for fields that instead have a value property, for example).

Maybe something like this:
Code:
// constructor for a wrapper for an object with innerHTML property:
function htmlObj( objOrId )
{
    // if a string is passed as the argument, assume it is an ID
    if ( typeof(objOrId) == "string" ) {
        this.domObj = document.getElementById(objOrId);
    } else {
        // otherwise assume it's a DOM object
        // (not the best assumption to make, but this is a silly demo)
        this.domObj = objOrId;
    }
    this.html = function( html ) 
    { 
        // if the argument is non-null, change the DOM object's innerHTML
        if ( html != null ) { this.domObj.innerHTML = html; }
        // and not matter what, always return its innerHTML
        return this.domObj.innerHTML;
    }
}

var box1 = new htmlObj( "divBox1" );
box1.html( "some html content" ); // pass non-null to set the innerHTML
// or 
alert( box1.html() ); // pass null or no arg to GET the innerHTML
Truthfully, I think that's more trouble than it is worth, but up to you.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.

Last edited by Old Pedant; 01-20-2013 at 07:14 AM..
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
matafy (01-20-2013)
Old 01-20-2013, 09:13 AM   PM User | #6
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
an option to call the html_() function were:
PHP Code:
html_.call($_('divBox1'), "some html content"); 
though whether that is sensible is another question.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 01-20-2013, 11:50 AM   PM User | #7
matafy
New Coder

 
Join Date: Apr 2009
Posts: 59
Thanks: 13
Thanked 0 Times in 0 Posts
matafy is an unknown quantity at this point
Quote:
Originally Posted by Old Pedant View Post
Code:
// constructor for a wrapper for an object with innerHTML property:
function htmlObj( objOrId )
{
    // if a string is passed as the argument, assume it is an ID
    if ( typeof(objOrId) == "string" ) {
        this.domObj = document.getElementById(objOrId);
    } else {
        // otherwise assume it's a DOM object
        // (not the best assumption to make, but this is a silly demo)
        this.domObj = objOrId;
    }
    this.html = function( html ) 
    { 
        // if the argument is non-null, change the DOM object's innerHTML
        if ( html != null ) { this.domObj.innerHTML = html; }
        // and not matter what, always return its innerHTML
        return this.domObj.innerHTML;
    }
}

var box1 = new htmlObj( "divBox1" );
box1.html( "some html content" ); // pass non-null to set the innerHTML
// or 
alert( box1.html() ); // pass null or no arg to GET the innerHTML


This looks like more than headache for me. I appreciate the help Old Pedant.

This is what I ended up doing for now or at least for when I get more coding experience...

PHP Code:
function $_(e){

    var 
hash e.match("#");
    var 
dot e.match(".");
    var 
_id e.replace(/#/g, "");
    
var _class e.replace(/./g"");

    if(
hash == "#"){
        return 
document.getElementById(_id);
    } else if(
dot == "."){
        if(
document.getElementsByClassName(_class) == _class){
            return 
document.getElementsByClassName(_class);
        } else {
            
document.getElementsByClass = function(_class){
                var 
cName = [];
                var 
tagName document.getElementsByTagName('*');
                for(var 
i=0i<tagName.lengthi++){
                    if(
tagName[i].className == _class){
                        
cName.push(tagName[i]);
                    }
                }
                return 
cName;
            }
            return 
document.getElementsByClass(_class)[0];
        }
    }
}
function 
html_(elementhtmlContent){
    
element.innerHTML htmlContent;
}

html_($_('.class'), "Some Content"); 
Thanks everyone!
matafy is offline   Reply With Quote
Old 01-20-2013, 03:30 PM   PM User | #8
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,799
Thanks: 30
Thanked 463 Times in 457 Posts
jmrker will become famous soon enough
Exclamation

You did not follow my directions in post #3 with this modification you made in post #4 ...
Code:
function $_(id){
   return document.getElementById(id);
}
function html_(c){
   return this.innerHTML = c;
}
The 'this' in your return is not the element you are trying to modify.
jmrker is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:42 PM.


Advertisement
Log in to turn off these ads.