I have button and <input type="button" onclick="test();" value="dugme"/> wont call test()

with alert(bla bla) directly works,but wont call my functions

why????????????????

here's the complete code

<html>
<head>
<title>moje vezbanje</title>
</head>

<body onload="alert('sdfsfd')">
<script type="text/javascript">
//alert("jhgfgh");
function klasa()	{


var nazivLinka = document.getElementById("prvo").value;
var url = document.getElementById("drugo").value;

//alert(nazivLinka);

//druga.createNode(input type="text" value="pocetni text");


function dodajLink()	{
	
	var link = document.createNode("a");//1 kreir element
	
	link.innerHTML = nazivLinka;//2 dodaj atribute
	link.href = url;
	document.getElementById("prva_kol").appendChild(link);//3 dodaj element na stranu

	}
	
function test()	{
	alert('123123');	
	}

function pozicija(id1,x,y)  {
id1.

}	
*/
}
</script>

<script>
var a = new klasa();
</script>

<table id="tabela" style="background-color:yellow; length:100%; width:100%;">
	<tr>		
		<td id="prva_kol" style="background-color:magenta; width:300px; height:300px;">11

		</td>
		<td id="druga_kol" style="background-color:red;">
		<input type="text" id="prvo" value="naziv linka"/>
		<input type="text" id="drugo" value="http://"/>
		<input type="button" onclick="test();" value="dugme"/>
		
		</td>
		<td style="background-color:blue;">33</td>	
	</tr>		
</table>


</body>
</html>
<html>
<head>
<title>moje vezbanje</title>
<script type="text/JavaScript">
<!--
/*
Delete the test() function from between the <body> tags
and place it here between the <head> tags
*/
function test()	{
	alert('123123');	
	}
//-->
</script>

</head>

<body onload="alert('sdfsfd')">
<script type="text/javascript">
//alert("jhgfgh");
function klasa()	{


var nazivLinka = document.getElementById("prvo").value;
var url = document.getElementById("drugo").value;

//alert(nazivLinka);

//druga.createNode(input type="text" value="pocetni text");


function dodajLink()	{
	
	var link = document.createNode("a");//1 kreir element
	
	link.innerHTML = nazivLinka;//2 dodaj atribute
	link.href = url;
	document.getElementById("prva_kol").appendChild(link);//3 dodaj element na stranu

	}
	

function pozicija(id1,x,y)  {
id1.

}	
*/
}
</script>

<script>
var a = new klasa();
</script>

<table id="tabela" style="background-color:yellow; length:100%; width:100%;">
	<tr>		
		<td id="prva_kol" style="background-color:magenta; width:300px; height:300px;">11

		</td>
		<td id="druga_kol" style="background-color:red;">
		<input type="text" id="prvo" value="naziv linka"/>
		<input type="text" id="drugo" value="http://"/>
		<input type="button" onclick="test();" value="dugme"/>
		
		</td>
		<td style="background-color:blue;">33</td>	
	</tr>		
</table>


</body>
</html>

See this link for more information about where to place the scripts.

It works.

So what is the point,which javascript code should be placed in <head> tags?

I saw the link in bottom now,so javascript onload code if written in head wont work?

I cant get this to work :S

it does cal the alert() but wont call my real function

I want to call onclick function that will create link in first column of the table

this func:

function dodajLink()	{
	
	var link = document.createNode("a");//1 kreir element
	
	link.innerHTML = nazivLinka;//2 dodaj atribute
	link.href = url;
	document.getElementById("prva_kol").appendChild(link);//3 dodaj element na stranu
	alert("url");
	}

and heres the complete code:

a.html:

<html>
<head>
<title>moje vezbanje</title>

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

<body>

<script language="javascript">
var a = new klasa();
</script>

<table id="tabela" style="background-color:yellow; length:100%; width:100%;">
	<tr>		
		<td id="prva_kol" style="background-color:magenta; width:300px; height:300px;">11

		</td>
		<td id="druga_kol" style="background-color:red;">
		<input type="text" id="prvo" value="naziv linka"/>
		<input type="text" id="drugo" value="http://"/>
		<input type="button" onclick="dodajLink();" value="dugme"/>
		
		</td>
		<td style="background-color:blue;">33</td>	
	</tr>		
</table>


</body>
</html>

here's the javascript code a.js:

function klasa()	{
//alert("jhgfgh");

var nazivLinka = document.getElementById("prvo").value;
var url = document.getElementById("drugo").value;

//alert(nazivLinka);

function dodajLink()	{
	
	var link = document.createNode("a");//1 kreir element
	
	link.innerHTML = nazivLinka;//2 dodaj atribute
	link.href = url;
	document.getElementById("prva_kol").appendChild(link);//3 dodaj element na stranu
	alert("url");
	}


}

I saw the link in bottom now,so javascript onload code if written in head wont work?

I don't know. I haven't worked with scripts in web pages for quite a while but I remember that we usually (maybe always?) had to define our functions in the head section and not the body to make them work when called.

But a script that does things like

<script type="text/JavaScript">
<!--
document.write("Hello World!")
//-->
</script>

would more likely go in the body section, depending where on the page you wanted it to write.

ok,anyway it's in separeted files now,in a.html and a.js and my question from post above still stands

why this func doesnt work?

function dodajLink() {

       

      var link = document.createNode("a");//1 kreir element

       

      link.innerHTML = nazivLinka;//2 dodaj atribute

      link.href = url;

      document.getElementById("prva_kol").appendChild(link);//3 dodaj element na stranu

      alert("url");

      }
Member Avatar for diafol

Have you tried the Javascript forum?

function klasa() is not closed, which means function test() is never declared. To close a klasa() add a } to line 18.

var nazivLinka = document.getElementById("prvo").value; is invalid. Remove the '.value' from the end. Do the same to var url = document.getElementById("drugo").value; But it still may not work if there are other errors. You need some kind of javascript error console in your browser to point you to what causes the code to break. I use Firefox version 3.5.9 which has an error console so you can know more than just 'it didn't work'.

Also look at <table id="tabela" style="background-color:yellow; length:100%; width:100%;"> Firefox doesn't accept 'length' as a table attribute.

ok,thank you for help!

I think that constructor is missing,and that should be fixed.

I use firebug for debuging.

ok,thank you for help!

I think that constructor is missing,and that should be fixed.

I use firebug for debuging.

You're welcome. I can see it's still not working. As you say, it may need some kind of constructor method. I haven't learned enough about objects yet. Good luck.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.