0

Hi !!
i faced problem with javascript. When I call function change() it applies only second element that is link2. I wanted when i click the link, the link name must appear to textfield.can anyone help??

here is code:

<html>
<body>
<form>
<a id="static" href="staticContentAdmin.ioml">link1</a></br>
<a id="static" href="staticContentAdmin.ioml">link2</a></br>
<input type="text" id="spec" name="content"/>
</form>
<script>
	var a = document.getElementsByTagName("a");
	for(var i=0;i<a.length;i++){
			a[i].href="#";
			var ale = a[i].innerHTML;
			a[i].onclick =change(ale);
	}
	
	function change(object){
		var inputobj = document.getElementById("spec"); 
		inputobj.value = object;
	}
</script>
</body>
</html>
4
Contributors
5
Replies
6
Views
8 Years
Discussion Span
Last Post by samlabs821
1

You weren't really calling the function, it starts running when the page loaded. I see that you are changing the href attribute to a hash inside the loop, so i modified your code to look like this:

<html>
<body>
<form>
<a  onclick="change(this)" id="static" href="#">link1</a></br>
<a  onclick="change(this)" id="static" href="#">link2</a></br>
<input type="text" id="spec" name="content"/>
</form>
<script>
	function change(object){
		var inputobj = document.getElementById("spec");
		inputobj.value = object.innerHTML;
	}
</script>
</body>
</html>

If u click on the links than that "innerhtml" is displayed in the textbox.

I had removed the loop since there was no need to have it, as all u want to achieve is click on the link and the value appear in the text box

1

Not shure using plain old JS. This works using mootools though:

$$('a').set('href', '#').addEvent('click', function() {
    $('spec').set('value', $(this).get('html'));
});
0

Not shure using plain old JS. This works using mootools though:

$$('a').set('href', '#').addEvent('click', function() {
    $('spec').set('value', $(this).get('html'));
});

:)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.