Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display text in div dynamically

I have searched on the forum and saw posts about changing text dynamically upon click. But in my case I want to change the display dynamically when loading the page from beginning. I already have a function that figure out what I should display:

function phone()
{
//code here
return phone;
}

And my question is how to display the returned phone number in the div below to replace the 1.888.888.8888 part. Can anyone offer some insights? Thank you!

<div class="add-info">
<span class="rightfloat">Order online <span class="red">or call 1.888.888.8888</span></span>
</div>
like image 623
walkman Avatar asked Mar 14 '13 15:03

walkman


People also ask

How do I put text inside a div?

Use the insertAdjacentText() method to append text to a div element, e.g. container. insertAdjacentText('beforeend', 'your text here') . The method inserts a new text node at the provided position, relative to the element it was called on. Here is the HTML for the examples in this article.

How do you dynamically change the content of a div?

text() . the string replace() method doesn't change the original string, it returns a new string. Is the text in the DOM at the time you are trying to change it? If its dynamic and hasn't loaded yet you will need to wait for the text to be present before trying to manipulate it.

How to set text in div using JavaScript?

Use the textContent property to change the text of a div element, e.g. div. textContent = 'Replacement text' . The textContent property will set the text of the div to the provided string, replacing any of the existing content.

How do I change the content of an HTML page dynamically?

There are a number of ways to dynamically manipulate HTML contents with Javascript: Directly change the contents with innerHTML and outerHTML. Create new HTML elements and insert them. Load and insert HTML contents with AJAX. Load data with AJAX, and generate a table or list. Dynamically load CSS files.

How to display dynamic content on the view in Python?

For displaying dynamic content on the view there are several approaches that can be used, some of them are: @if else or @switch case or @for or @foreach inside the view itself for doing a conditional construct, we can also call it inline code since it is written inside the view. This is for performing a simple conditional construct.

How do you center a text in a Div?

Using the Text-Align Property. In most cases, you can center text horizontally in a div using the text-align property and defining it with the value “center.”. Say you want to create a div element with a short paragraph inside and a yellow border around it. In your HTML, you’d give the div a class name like “center.”.

How to display dynamic content on the view in Salesforce?

For displaying dynamic content on the view there are several approaches that can be used, some of them are: @if else or @switch case or @for or @foreach inside the view itself for doing a conditional construct, we can also call it inline code since it is written inside the view.


2 Answers

I would change the HTML to add another <span> tag around the phone number and give that span tag an id attribute in order to access it easily (broke it up on separate lines to reduce scrolling):

<div class="add-info">
    <span class="rightfloat">
        Order online <span class="red">
            or call <span id="contact-number"></span>
        </span>
    </span>
</div>

Then after the page loads update the span with whatever value you want:

window.onload = function() {
    document.getElementById('contact-number').innerHTML = PHONE_NUMBER_VALUE;
}

In JQuery, it would be:

$(document).ready(function () {
    $('#contact-number').html(PHONE_NUMBER_VALUE);
});
like image 84
talemyn Avatar answered Nov 03 '22 00:11

talemyn


You can,

<body onload="phone();">
    <div class="add-info">
        <span class="rightfloat">Order online <span class="red">or call
            <span id="phone"></span>
        </span>
    </div>
</body>

And set the value when the function runs;

function phone() {
    document.getElementById("phone").innerHTML = "1.888.888.8888";
}
like image 32
Alex K. Avatar answered Nov 02 '22 23:11

Alex K.