/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] creating html with javascript

Im really stuck with this one.

I have an array with 5 elements : [word1,word2,word3,word4,word5]

when the user presses a button:SHOWINFO it needs to show the 5 words with buttons next to it. like this.

BUTTON: SHOW INFO


——–

TABLE

WORD1 | BUTTON1
WORD2 | BUTTON2
WORD3 | BUTTON3
WORD4 | BUTTON4
WORD5 | BUTTON5

I have something like

function showInfo(){

var a = new Array(word1,word2,word3,word4,word5)

var button1 = document.createElement(“button”);
button1.innerHTML = a[1];
button1.id = a[1]

var button2 = document.createElement(“button”);
button2.innerHTML = a[1];
button2.id = a[1]

div.appendChild(button1,button2)

}

to post a comment
JavaScript

20 Comments(s)

Copy linkTweet thisAlerts:
@PalliauthorSep 26.2008 — This sort of works, but the layout is wrong, i need to have the buttons next to each word not above. And when each button is pressed i want it to alert("buttonID"). Is this possible, i feel i am really close but cant get my head round it



function display(ar){


for(var i=0; i<ar.length; i++) {

var divTag = document.createElement("div");
var button = document.createElement("button");

divTag.id = "div"+i;
button.id = "button"+i

divTag.setAttribute("align","left");
divTag.style.margin = "0px auto";
divTag.className ="dynamicDiv";

divTag.innerHTML = ar[i];
button.innerHTML = "Press Me";

document.body.appendChild(button);
document.body.appendChild(divTag);

}



}
Copy linkTweet thisAlerts:
@firstborn2501Sep 26.2008 — Palli, for the layout use 'span' elements instead of 'div'. Divs are block elements while spans are inline.

For the alert add (just after the 'button.id=" line):

button.setAttribute('onclick', 'alert(this.id);';

I think that should do it for you.
Copy linkTweet thisAlerts:
@ZeroKilledSep 26.2008 — setting an event with setAttribute method wouldn't work on msie6. i would use the old version:

element.event = function(param...){

body...

}
Copy linkTweet thisAlerts:
@firstborn2501Sep 26.2008 — Good point!
Copy linkTweet thisAlerts:
@PalliauthorSep 26.2008 — Thank you both for your reply


firstborn2501, when i use span they are all on one line like you said, but how what would i need to do to have one textlabel and one button per line.


ZeroKilled, could you explain a bit more what you mean by :

element.event = function(param...){

body...

}


when i try this it wont work....

button.event = function(){alert(this.id)}
Copy linkTweet thisAlerts:
@dgurovich_mediaSep 26.2008 — I would go with a slightly different method that might give you more granularity. The code below is shows a separation of the information, and allows you to use any size array that you want, doesn't matter. You can take it further by passing in the array instead of hard-coding it, and well there's lots of stuff you can do here. Hope this helps:

//Create a button row class that holds the info

function ButtonRow(word) {

this.word = word;

this.a = word;

this.b = new WordButton(word).value;

this.lineval = '<div><span>' + this.a + '</span><span>' + this.b + '</span></div>'

}

//Create a button maker

function WordButton(word) {

this.value = '<button onclick="alert('' + word + '');" > click</button>';

}

//iterate over your array and you get your name/button pairings.

function buildButtons() {

var a = new Array("Bill", "Bob", "Hal", "Carl", "Lenny")

for(var i=0; i<a.length;i++){

var btn = new ButtonRow(a[i]);

document.write(btn.lineval);

}

}



Finally, here's the stuff on for the HTML



<div>

<button onclick="buildButtons();">BUILD</button>

</div>
Copy linkTweet thisAlerts:
@ZeroKilledSep 26.2008 — what i meant that is the structure or syntax you may use. however [b]event[/b] should be replaced to any valid element's event, in your case onclick:

<i>
</i>button.[color='sienna']onclick[/color] = function(){alert(this.id)};
Copy linkTweet thisAlerts:
@felgallSep 27.2008 — I would go with a slightly different method that might give you more granularity. The code below is shows a separation of the information, and allows you to use any size array that you want, doesn't matter. You can take it further by passing in the array instead of hard-coding it, and well there's lots of stuff you can do here. Hope this helps:

//Create a button row class that holds the info

function ButtonRow(word) {

this.word = word;

this.a = word;

this.b = new WordButton(word).value;

this.lineval = '<div><span>' + this.a + '</span><span>' + this.b + '</span></div>'

}

//Create a button maker

function WordButton(word) {

this.value = '<button onclick="alert('' + word + '');" > click</button>';

}

//iterate over your array and you get your name/button pairings.

function buildButtons() {

var a = new Array("Bill", "Bob", "Hal", "Carl", "Lenny")

for(var i=0; i<a.length;i++){

var btn = new ButtonRow(a[i]);

document.write(btn.lineval);

}

}



Finally, here's the stuff on for the HTML



<div>

<button onclick="buildButtons();">BUILD</button>

</div>
[/QUOTE]






Why would someone go back to that old fashioned approach to creating HTML in JavaScript when they already have code that uses the standard way of doing it using [b]document.createElement[/b]?
Copy linkTweet thisAlerts:
@PalliauthorSep 27.2008 — Thanks for the replies.....

but how could i display it so that one label and one button are on the same line???
Copy linkTweet thisAlerts:
@FangSep 27.2008 — Show the code you are using.
Copy linkTweet thisAlerts:
@PalliauthorSep 27.2008 — Fang, at the moment im using this code:



function display(ar){


for(var i=0; i<ar.length; i++) {

var spanTag = document.createElement("span");

var button = document.createElement("button");

spanTag .id = "span"+i;

button.id = "button"+i

spanTag .setAttribute("align","left");

spanTag .style.margin = "0px auto";

spanTag .className ="dynamicDiv";

spanTag .innerHTML = ar[i];

button.innerHTML = "Press Me";

button.onclick = function(){alert(this.id)};





document.body.appendChild(button);

document.body.appendChild(spanTag );



}





}
Copy linkTweet thisAlerts:
@FangSep 27.2008 — What is ar? What fires the function?
Copy linkTweet thisAlerts:
@PalliauthorSep 27.2008 — Sorry,

here is the full code....

HTML:

[CODE]<html>

<head>

<script type="text/javascript" src="showButtonsScript.js"></script>

</head>

<body>
<input type=button value=press me onclick = showButtons()>


<div id = "buttonsDiv"</div>



</body>
</html>[/CODE]


JS:


[CODE]

function showButtons(){
var ar = new Array ()
ar = ["one","two","three","four","five"];
alert(ar);
createButtons(ar);
}




function createButtons(ar){




for(var i=1; i<ar.length; i++) {

var spanTag = document.createElement("span");
spanTag.setAttribute("align","left");
spanTag.style.margin = "0px auto";
spanTag.className ="dynamicDiv";
spanTag.innerHTML = ar[i];
spanTag.id = "span"+i;


var button = document.createElement("button");
button.id = ar[i];
button.innerHTML = "Press Me";
button.onclick = function(){alert(this.id)};

document.getElementById("buttonsDiv").appendChild(button);
document.getElementById("buttonsDiv").appendChild(spanTag);

}


}


[/CODE]



So at the moment all the buttons and labels are show in one line, but i want them to be shown one label and one button on a line each, or in a neat table, could you please help me on this one.

Thanks Fang ?
Copy linkTweet thisAlerts:
@FangSep 28.2008 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;title&gt;DOM&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8"&gt;

&lt;script type="text/javascript"&gt;
function showButtons(){
var ar = ["one","two","three","four","five"];
alert(ar);
createButtons(ar);
}

function createButtons(ar){
for(var i=1; i&lt;ar.length; i++) {
var labelTag = document.createElement("label");
labelTag.className ="dynamicDiv";
labelTag.setAttribute('for', ar[i]);

<i> </i> var button = document.createElement("button");
<i> </i> button.setAttribute('type', 'button');
<i> </i> button.id = ar[i];
<i> </i> button.appendChild(document.createTextNode('Press Me'));
<i> </i> button.onclick = function(){alert(this.id)};

<i> </i> document.getElementById("buttonsDiv").appendChild(labelTag);
<i> </i> labelTag.appendChild(button);
<i> </i> labelTag.appendChild(document.createTextNode(ar[i]));
<i> </i>}
}
&lt;/script&gt;

&lt;style type="text/css"&gt;
label {display:block;}
&lt;/style&gt;

&lt;/head&gt;
&lt;body&gt;
&lt;div&gt;
&lt;input type="button" value="press me" onclick="showButtons();"&gt;
&lt;/div&gt;

&lt;div id="buttonsDiv"&gt;&lt;/div&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@PalliauthorSep 28.2008 — Fang, Thanks for the working code, however when i tried it out i could click anywhere on a whole line streching across the browser (even where there was no text or button) and it would trigger the button function call!

I have managed to make this code:

[CODE]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<title>DOM</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

<script type="text/javascript">

function showButtons(){
var ar = ["one","two","three","four","five"];
test(ar);
}




function test(ar){

for(var i=0; i<ar.length; i++){

var table = document.createElement("table");
var tr = table.insertRow(0);

var td = tr.insertCell(0);
td.innerHTML = ar[i];

td = tr.insertCell(1);
var button = document.createElement("button");
button.id = ar[i];
button.appendChild(document.createTextNode('Press Me'));
button.onclick = function(){alert(this.id)};
td.appendChild(button);

document.getElementById("buttonsDiv").appendChild(table);
}

}

</script>


</head>
<body>
<div>
<input type="button" value="press me" onclick="showButtons();">
</div>

[/CODE]




How can i set the size of the cells so that if the labels are different lengths the buttons will still be aligned properly. Thanks for all the help guys.
Copy linkTweet thisAlerts:
@FangSep 29.2008 — function createButtons(ar){
for(var i=1; i&lt;ar.length; i++) {
var divTag = document.createElement("div");
divTag.className ="dynamicDiv";

<i> </i> var button = document.createElement("button");
<i> </i> button.setAttribute('type', 'button');
<i> </i> button.id = ar[i];
<i> </i> button.appendChild(document.createTextNode('Press Me'));
<i> </i> button.onclick = function(){alert(this.id)};

<i> </i> document.getElementById("buttonsDiv").appendChild(divTag);
<i> </i> divTag.appendChild(button);
<i> </i> divTag.appendChild(document.createTextNode(ar[i]));
<i> </i>}
}
Copy linkTweet thisAlerts:
@PalliauthorSep 29.2008 — Good Idea, THanks Fang

RESOLVED
Copy linkTweet thisAlerts:
@realman2007_1loOct 11.2008 — I'm trying rewrite a script that load ads links and use proxy but I can't figure what I'm doing wrong.Also the .exe is always coming up with erorrs.Later I will upload script so someone can rewite it and give me more advice on how to recode it.

I want the script to run as a program without firefox as the browser.If anyone know how to make a PTC,PTR & PPC bot please please help me.
Copy linkTweet thisAlerts:
@realman2007_1loOct 11.2008 — These Are The Code I'm Having Problems With:I Can't Load Proxy List With Program Or Get It To Work.

(1)

// ==UserScript==

// @name General Bux Script

// @namespace AutoclickerMan

// @email autoclickerm.siteburg.com.com

// @description auto click ads for you in 26bux sites

// @version 0.0551

// @date 25.01.2008

// @sitecounter 26

// @include http://*advercash.net/adverts.php

// @include http://*
cashwebs.net/adverts.php

// @include http://*titanclicks.com/adverts.php

// @include http://*
turbobux.com/adverts.php

// @include http://*bux.to/newsurf.php

// @include http://*
bux3.com/surf.php

// @include http://*easy.tc/surf.php

// @include http://*
tiserbux.net/surf.php

// @include http://*earnclick.net/surf.php

// @include http://*
linksbux.com/surf.php

// @include http://*instantad.org/surf.php

// @include http://*
buxpro.com/surf.php

// @include http://*daddybux.com/surf.php

// @include http://*
dailyclicks.biz/surf.php

// @include http://*hits4bux.com/surf.php

// @include http://*
spainbux.com/surf.php

// @include http://*buxer.org/surf.php

// @include http://*
earn-in.info/viewads.php

// @include http://*tobux.com/surf.php

// @include http://*
buxa.in/surf.php

// @include http://*moonbux.com/surf.php

// @include http://*
sebasbux.com/surf.php

// @include http://*rosebux.com/surf.php

// @include http://*
allstarsbux.com/surf.php

// @include http://*tugabux.net/surf.php

// @include http://*
masterbux.com/surf.php

// @include http://*.bux.to/surf.php

// @include http://*
.bux.to/surf.*

// @include http://*
.*.*/surf.php

// @include http://*.*.*/surf.php

// @include http://*
.bux.to/?r=key.domain

// @include http://*.bux.to/register.php

// @include http://*
.bux.to/register.*

// @include http://*
.bux.*/register.php

// @include http://*
view*.php*

// @include http://*.bux.to/surf.php

// @include http://*
.bux.to/login.php*

// @include http://www.prismarclicks.com/surf.php

// @include http://www.prismarclicks.com//index.php

// @include http://www.prismarclicks.com/view.php?ad*


// @include http://www.fairbux.net/surf.php

// @include http://www.fairbux.net/index.php

// @include http://www.fairbux.net/view.php?ad*

// @include http://www.rocashbux.info/cashads.php?ad=*


// @include http://*.world-bux.to/surf.php

// @include http://*
.world-bux.to/index.php

// @include http://*.world-bux.to/view.php?ad=*

// @include http://www.bienpagado.com/bux/surf.php

// @include http://www.crewbux.com/surf_banners.php

// @include http://www.perfectbux.com/index.php?page=surf-ads

// @include http://www.thinkbux.com/surf_ads_think.php

// @include http://*.foxcash.net/register.php

// @include http://*
.foxcash.net/register.*

// @include http://*
.foxcash.*/register.php

// @include http://*
.07bux.net/register.php

// @include http://*.07bux.net/register.*

// @include http://*.07bux.*/register.php

// @include http://*.bux3.com/register.php

// @include http://*
.bux3.com/register.*

// @include http://*
.bux3.*/register.php

// @include http://*/*

// ==/UserScript==

var site=document.domain;

if (site.match(/www./g)){var site=site.replace('www.', '');}


var basari = /success/gi;

GM_setValue('baslat',1);

GM_setValue('sure',5);

GM_setValue('dil','en');

var baslat = GM_getValue('baslat');

var sure = GM_getValue('sure');

var dil = GM_getValue('dil');



//Genel Kontrol

var kont1=/view/;

var kont2=/ad/;

var kont3=/=d/;

var ac =/nothing/;

switch(dil)

{

case 'tr':

var basla = 'Basla';

var temizle = 'Temizle';

var basladi = 'Basladi.';

var kontrol = 'Kontrol ediliyor: ';

var aciliyor = 'Sayfa aciliyor:';

var tamam = 'Tamamland&#305;. bir sonrakine geciliyor..';

var beklenniyor = 'Pencere acildi, bekleniyor...';

var bitti1 = 'Tum sayfalara gidildi. ';

var bitti2 = ' dakika içinde sayfa yenilenecek';

var sayfalar = 'Gidilecek sayfalar: ';

var versiyon = 'Versiyon 0.0551';

break;
Copy linkTweet thisAlerts:
@realman2007_1loOct 11.2008 — var currentWindow;

var currentElement;

var links;

var watcher;

var surf = document.evaluate(surfTable, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;

var container = document.createElement('div');

container.appendChild(document.createElement('br'));
container.appendChild(document.createElement('br'));
container.appendChild(document.createElement('br'));


var button = document.createElement('button');
button.innerHTML = basla;
button.addEventListener('click', processLink, false);
container.appendChild(button);

var clear = document.createElement('button');
clear.innerHTML = temizle;
clear.addEventListener('click', clearDebug , false);
container.appendChild(clear);

//var dilsec = document.createE

var ver = document.createElement('button');
ver.innerHTML = versiyon;
ver.setAttribute('disabled', 'disabled');
container.appendChild(ver);

container.appendChild(document.createElement('br'));

var progress = document.createElement('textarea');
progress.style.width = "100%";
progress.style.height = "20em";
progress.setAttribute('readonly', 'true');
container.appendChild(progress);

surf.parentNode.insertBefore(container, surf);

startSurf();



//Fonksiyonlar

function yenile(dakika)

{

window.setTimeout('document.location.reload();', dakika * 60 * 1000);

debug(bitti1 + dakika + bitti2)

}

function adwatchDone(){

debug(tamam);

var strike = document.createElement('strike');
strike.innerHTML = currentElement.innerHTML;
currentElement.parentNode.replaceChild(strike, currentElement);

setTimeout(function(){
currentWindow.close();
processLink();
}, 2000);

}



function watchLocation(){

//currentWindow.onLoad.apply(currentWindow,[]);

watcher = setInterval(function(){

var frames = currentWindow.document.getElementsByName('success');
var frame = frames[0];
debug(kontrol + frame.contentWindow.location.href);
if(frame.contentWindow.location.href.match(basari))
{
adwatchDone();
clearInterval(watcher);
}
}, 5000);


currentWindow.currentElement = currentElement;

}


function processLink(){

button.setAttribute('disabled', 'disabled');

var element = links.pop();

if(element){
debug(aciliyor + element.href);
currentElement = element;
var url = element.getAttribute('href');
currentWindow = window.open(url);
currentWindow.addEventListener('DOMContentLoaded', watchLocation, false);
debug(bekleniyor);
}else{
yenile(sure);
}

}

function getLinks(){

var links = document.evaluate(linksx, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);

var work = [];

var links2= document.evaluate(linksx2, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
for(var i=0;i<links2.snapshotLength;i++){
var item = links2.snapshotItem(i);
//Anti Anti Cheat
if (aac(item) && filtre(item)){work.push(item);}
}

for(var i=0;i<links.snapshotLength;i++){
var item = links.snapshotItem(i);
//Anti Anti Cheat
if (aac(item) && filtre(item)){work.push(item);}
}
return work;

}

function startSurf(){

debug(basladi);

links = getLinks();

if (links ==''){yenile(sure);}

else

{

debug(sayfalar +'('+ links.length +') '+ links);

links.reverse();

if (baslat==1){

processLink();}

}

}

function clearDebug(){

progress.value = "";

}

function debug(str){

if(progress.value.length >= 1){

progress.value += "n" + str;

}else{

progress.value = str;

}

progress.scrollTop = progress.scrollHeight;

}

function yenile(dakika){

window.setTimeout("document.location.reload();", dakika * 60 * 1000);

button.setAttribute('disabled', 'disabled');

debug(bitti1 + dakika + bitti2)

}

function filtre(baglanti){

var kon1=new RegExp(kont1);
var kon2=new RegExp(kont2);
var kon3=new RegExp(kont3);
var k1=kon1.test(baglanti);
var k2=kon2.test(baglanti);
var k3=kon3.test(baglanti);

if (k1 && k2 && k3){return true;}
else {return false;}

}

function aac(baglanti){

var konaac=new RegExp(ac);

var kaac=konaac.test(baglanti);

if(kaac){return false;}

else {return true;}

}



(2)

{rtf1ansiansicpg1252deff0deflang1033{fonttbl{f0fromanfcharset0 Times New Roman;}{f1fswissfcharset0 Arial;}}

{*generator Msftedit 5.41.15.1507;}viewkind4uc1pardsb100sa100bf0fs24 CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, Falseb0par

pardf1fs20par

}

(Autorun Setting)

(3)

{rtf1ansiansicpg1252deff0deflang1033{fonttbl{f0fromanfcharset0 Times New Roman;}{f1fswissfcharset0 Arial;}}

{*generator Msftedit 5.41.15.1507;}viewkind4uc1pardsb100sa100bf0fs24 [autorun]par

open=AutoClickerman.batpar

action=Run Scriptpar

shellopencommand=AutoClickerMan.batb0par

pardf1fs20par

}


What Am I Doing Wrong

The Program ItselfC:Documents and SettingsAdministratorDesktopAUtoClickerMan
×

Success!

Help @Palli spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...