0

I have a simple function constructor and want to assign a DOM element ID to one of the properties. When this is done outside of a constructor you have to put the ID in quotation marks IE getElementById(" whatever ");

In my constructor I am not sure if I need to do this, how do add them, nor if I am formatting this correctly.

<form id="form">
<input type="text" id="bookText" value="Book">
</form>

<script>

function Item(itemName,itemDomID){
this.itemName = document.getElementById(this.itemDomID);   
};

var mybook = new Item(book,bookText);       // new object.


</script>
2
  • Wait, I am slightly confused. Do you want to save the id? cause at the moment, you are assigning the element to itemName
    – Johan
    Sep 28, 2012 at 6:39
  • 1
    Your function looks like a useless wrapper for document.getElementById. Rephrase your question. Sep 28, 2012 at 6:41

3 Answers 3

1

I have a simple function constructor and want to assign a DOM element ID to one of the properties. When this is done outside of a constructor you have to put the ID in quotation marks IE getElementById(" whatever ");

An element ID is a string, assigning a string to an object property does not require a function, just an assignment:

mybook.book = 'bookText';

When passing values using variables, just use the variable name. The expression will be evaluated to return the value. The constructor should be (note changes to original):

function Item(itemName, itemDomID){

    this[itemName] = document.getElementById(itemDomID);   

}

Note the use of square brackets to assign the value of itemName to the new property name, rather than the literal string "itemName".

So when calling the constructor, string values can be passed:

var mybook = new Item('book', 'bookText');

The values will be assigned to the variables in the function in order, so itemName is assigned the value "book" and itemDomID is assigned the value "bookText".

Inside the constructor, the new object will be given a property named "book" with a value of whatever is returned by document.getElementById('bookText'), which will either be a reference to an element, or null if no element with that ID is found.

2
  • How come: document.write(mybook.itemName); displays undefined
    – William
    Sep 28, 2012 at 7:56
  • Because myBook doesn't have an itemName property, the property name is set to the value of itemName, which is book. So try mybook.book.
    – RobG
    Sep 28, 2012 at 22:17
1

Is your constructor parameter should be string?
try this in jsfriddle

<form id="form">
<input type="text" id="bookText" value="Book">
</form>

​function Item(itemName,itemDomID){
    this.itemName = document.getElementById(itemDomID).value;   
};

var mybook = new Item("book","bookText");       // new object.
1
  • I didn't intend for the .value of the DOM element to be passed but thanks for this regardless. I learned something inadvertently. And yes, it should have been string.
    – William
    Sep 28, 2012 at 22:16
0
document.getElementById(this.itemDomID);

This will return the javascript object. you can use like

var refObj=document.getElementById(this.itemDomID);

var objName=refObj.id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.