0

i am using javascript to change the text of div tag on run time.

how can this be done..

my div tag is as:

<div id="topdiv" style="color:Blue" onmouseover="button1();">
    <input type="button" id="btndiv"  onclick="edit1();"/>
     Div Tag
    </div>

i wnt the user to input text on runtime in div and that should be displayed in div. can someone help me..

3
  • No, not like that. As @gnur has stated, innerHTML is NOT a Javascript function but a property which you have to assign to. Commented May 26, 2011 at 9:44
  • now i want to change at run time. means as i click on button, a cursor should pop-up and the text should be entered at runtime.. Commented May 26, 2011 at 9:47
  • So do it, and ask a new question if you get stuck. Commented May 26, 2011 at 9:53

10 Answers 10

3

It should be innerHTML. innerHTM is not a javascript function.

Sign up to request clarification or add additional context in comments.

1 Comment

@kawade: is this a new question? You'll probably get a quicker answer if you move this comment into a new question.
2
  1. You don't get a magic variable just by having an element with an id. var something = document.getElementById('some-id')
  2. The property is called innerHTML not innerHTM
  3. innerHTML is a string variable not an function. Assign a value to it with =, don't try to call it with ()

2 Comments

now i want to change at run time. means as i click on button, a cursor should pop-up and the text should be entered at runtime..
So do it, and ask a new question if you get stuck.
2
function edit1() {
    alert('you are in edit1');
    document.getElementById('topdiv').innerHTML = 'hello';
}

and with proper error handling:

function edit1() {
    alert('you are in edit1');
    var topDiv = document.getElementById('topdiv');
    if (topDiv != null) {
        topDiv.innerHTML = 'hello';
    } else {
        alert('topdiv is nowhere to be found in this DOM');
    }
}

3 Comments

now i want to change at run time. means as i click on button, a cursor should pop-up and the text should be entered at runtime..
@kawade, where should this text by entered?
dimitrov: when i click button, a cursor should blink on div text and as user edit the text it should be displayed on html page
1

Try document.getElementById('topdiv').innerHTML = "Hello"

Comments

1

To get the div you should use document.getElementById('topdiv'). There is indeed a WebKit feature, that elements with an ID are automatically expanded as global variables, but it's highly questionable, that this becomes mainstream.

Then, innerHTM should read innerHTML, and you assign directly:

foo.innerHTML = "hi there"

Comments

1

you should use

document.getElementById('topdiv').innerHTML = 'hello';

Comments

1

You should use references instead of ID's, using this.
In that case this means the node that triggers the event.

<div style="color:Blue" onmouseover="button1(this);">
    <input type="button" onclick="edit1(this);"/>
    Div Tag
</div>

function button1(divRef){
  //divRef is the reference to the DIV
}
function edit1(inputRef){
  //inputRef is the reference of the INPUT
  //inputRef.parentNode is the reference to the DIV
}

Comments

0
function edit1() {
        alert('you are in edit1');
        document.getElementById('topdiv').innerHTML = 'hello';
    }

This should work by specifying the id

Comments

0

In standard JavaScript usage you'd do as per @DarinDimitrov 's answer.

document.getElementById("topdiv").innerHTML = ('hello');

Once you're happy with JavaScript I would suggest you look at the JQuery libraries - the powerful syntax will let you write short, neat code like this:

$("#topdiv").html('hello');

Comments

0

Your file

<div id="topdiv" style="color:Blue" onmouseover="button1();"> Div Tag</div>
<form><input type="button" id="btndiv" value="Edit" onClick="window.open('t2.html','popuppage','width=850,toolbar=1,resizable=1,scrollbars=yes,height=700,top=100,left=100');" value="Open popup"/></form>

t2.html file

function sendValue (s){var selvalue = s.value;window.opener.document.getElementById('topdiv').innerHTML = selvalue;window.close();}

<form name="selectform"><input name="details" value=""><input type=button  value="Copy input to parent opener" onClick="sendValue(this.form.details);"></form>

Got Idea from this source enter link description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.