views:

34

answers:

2

I've an HTML page like following:

<html>
    <body>
         <div id="emaildiv" class="main" style="width:150px;height:80px;"></div>
    </body>
</html>

I want to add a DIV with a TEXT and BUTTON in to it dynamically. All these are made by using the following javascript.

function newEmailForContact() {
    var parentDiv = document.getElementById('emaildiv');
    var newEmailDiv = document.createElement('div');
    newEmailDiv.setAttribute('id','newEmailDiv');
    newEmailDiv.setAttribute('style', 'display:table;');

    var newEmail = document.createElement('INPUT');
    newEmail.setAttribute('type','text');
    newEmail.setAttribute('name','newEmail');
    newEmail.setAttribute('class', 'textfield_addemail');
    newEmail.setAttribute('id','newEmail');

    var addEmailBtn = document.createElement('INPUT');
    addEmailBtn.setAttribute('type','button');
    addEmailBtn.setAttribute('name','addEmail');
    addEmailBtn.setAttribute('id','addEmail');
    addEmailBtn.setAttribute('value', 'Add');
    newEmailDiv.appendChild(newEmail);
    newEmailDiv.appendChild(addEmailBtn);

    parentDiv.appendChild(newEmailDiv);
}

But the TEXT and Button are displayed in two different rows.

EDIT :- CSS class :-

.textfield_addemail    {
    border-width: 1px;
    border-style: solid;
    border-color: #999999;
    background-repeat: repeat-x;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    color: #333333;
    width: 120px;
    height: 15px;
    }

I want to display both in same row. Any solution?

A: 

Leave out the

newEmailDiv.setAttribute('style', 'display:table;');

Then your span will be displayed as inline and your button should appear next to it

JochenJung
I removed that line, but still its not working :(.
Joe
+2  A: 

The problem is your initial <div> isn't wide enough at 150px, just increase the size a bit, like this:

<div id="emaildiv" class="main" style="width:250px;height:80px;"></div>​

You can see a working/updated example with only this change here.

Nick Craver
I've made changes to the width as you said, but still its displaying in two different rows. There are lots of space available to fit the button.
Joe
@Joe - Given your example that should work...do you have *additional* CSS rules that apply to these elements?
Nick Craver
Try to apply a smaller width to your input field: newEmail.setAttribute('size','8');
JochenJung
Yes I'm using a CSS class. I've updated it in the Question. Please check it
Joe
@Joe - Here's an updated example: http://jsfiddle.net/nick_craver/SxkEg/ Does this not work either? I tested in Chrome/FF/IE8 here, are you using something else? If the example works there's still something outside your question's code/styles affecting this.
Nick Craver
@nick-craver - I'm using FF2 and I'm locked to it. I cant use anything else.
Joe
@Joe - Is the example page I linked in the last comment doing the same thing?
Nick Craver
@nick-craver - I'm getting a right pane and a main area of a web page. In main area, I can see two sections "JavaScript" and "result". But they don't have anything in it to display.
Joe
@Joe - That shouldn't be the case, any addons running?
Nick Craver
@nick-craver - In FF, yes... firebug and adblock+....
Joe
@Joe - does turning those off let it run correctly?
Nick Craver
@nick-craver - I got a new machine with FF latest on it. :) . I've tested the above links from it. and both of them are working correctly. But when I used it with my web page. Same problem repeats. :(
Joe
@nick-craver - I forgot to mentioned that its a joomla page where I'm using these sripts.
Joe
@nick-craver - At last I got it working in FF3. As you mentioned in your first reply it was the width caused the problem. But I don't know why it have problem with FF2. Thank you.
Joe