javascript lesson 2 / function to change the value of an element

html element
<p id=”sample_paragraph”> paragraph content </p>

javascript 
//defining a variable to assign the function

var changeParagraphValue = function ( paragraph_content ){
    //paragraph element is found with its id and assigned to a variable
    var paragraph = document.getElementById(“sample_paragraph”);  
    //innerHTML is the property to receive an element’s html value
    var paragraph_value = paragraph.innerHTML; 
    //printing the first value of the paragraph before we change it
    document.writeln(“first value = “+ paragraph_value + “<br>”);
    //changing the content with the given value
    paragraph.innerHTML = paragraph_content;
    paragraph_value = paragraph.innerHTML; 
    //printing out the new value of the paragraph
    document.writeln(“new value = ” + paragraph_value + “<br>”);
    
}

//calling the function to change the paragraph’s content to “new content”    
changeParagraphValue(“new content”);

test

Advertisements

About Firat Karakusoglu

IIT Tech.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: