...

View Full Version : How can I do?



willjs
01-20-2006, 04:11 PM
Hi,

Well, I need to create a new html button, but I need checking before (with a javascript function) which is type of button.

How can I do this?

Example:

<script>
var navegador_user;
var espaco;

navegador_user = navigator.userAgent;
espaco = navegador_user.indexOf(' ');

navegador_user = navegador_user.substring(0,espaco);

if (navegador_user == "Mozilla/2.0")
{
</script> // It's wrong, but this is the idea!!
<input name="Submit" type='submit' class='select_hand' onClick='javascript:salvar()' value="Salvar">
<script> // It's wrong!!
}
else
{
</script>
<input type='button' value='Salvar' class='select_hand'>
<script>
}
</script>

Kor
01-20-2006, 04:47 PM
Try this. Use a submit button default written in HTML, and change the type only when navegador_user is other than "Mozilla/2.0". This way you will have a submit button even for users which might have javascript disabled.

Try this:


<script type="text/javascript">
var navegador_user;
var espaco;
navegador_user = navigator.userAgent;
espaco = navegador_user.indexOf(' ');
navegador_user = navegador_user.substring(0,espaco);
function changeType(){
if(navegador_user != "Mozilla/2.0"){
var but = document.getElementById('submitMe');
but.type='button'
but.onclick=salvar;
}
}
onload = changeType;
</script>
...
<input name="submitMe" id="submitMe" type="submit" class="select_hand" value="Salvar">
...

willjs
01-24-2006, 01:22 PM
Kor,

I've had a problem with this command:

but.type='button'

I tried: but.setAttribute('type','button');

But didn't function. Do you know why?

SpirtOfGrandeur
01-24-2006, 01:40 PM
You cannot change the type of an HTML input after it has been added to the DOM. The property becomes "Read Only".

Kor
01-24-2006, 04:44 PM
You cannot change the type of an HTML input after it has been added to the DOM. The property becomes "Read Only".

Hm... It looks like only IE refuses to do that. In Moz it works ok:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
function salvar(t){
alert(t)
}
function changeType(){
var but = document.getElementById('submitMe');
but.setAttribute('type','button');
but.onclick=function(){
salvar(this.type)
}
}
onload = changeType;
</script>
</head>
<body>
<input id="submitMe" type="submit" value="Salvar">
</body>
</html>

I'll think of another approach.... maybe on create/remove elements, rathre than change the attribute...

Kor
01-24-2006, 04:54 PM
I would switch through another approach... I would have put in HTML a submit button and launch whichever function onsubmit (in form tag) rather that onclick( in the button tag). Now, in the function launched onsubmit, I would have had inserted that condition:

if(navegador_user != "Mozilla/2.0"){
... do something
}

and that is all

SpirtOfGrandeur
01-24-2006, 05:30 PM
Hm... It looks like only IE refuses to do that. In Moz it works ok:


Kor, from all that I have read on the subject is that IE does it this way because all their HTML controls are actual windows controls. And those do not allow the change on a type. This might/might not be fixed in IE 7.0 and cannot be expected to work. The trick here is to use the DOM to create a new element and remove the old element.


function changeType(){
var but = document.getElementById('submitMe');
var oInput = document.createElement('input');
oInput.type = 'button';
oInput.onclick=function(){
salvar(this.type)
}
but.parentNode.insertBefore( oInput, but );
but.parentNode.removeChild( but );
}

Note I did not validate this code. I do not have time but it is the correct logic.

Kor
01-24-2006, 09:10 PM
The trick here is to use the DOM to create a new element and remove the old element.

That was my first thought too...


I'll think of another approach.... maybe on create/remove elements, rather than change the attribute...

But I tried to think deeper, and reckon that there is no need to create an element unless you are obliged to. This case I guess it is not necessary...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum