Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8

Thread: How can I do?

  1. #1
    New Coder
    Join Date
    Dec 2005
    Location
    São Paulo, Brasil
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question How can I do?

    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>
    Tks
    William

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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:
    PHP Code:
    <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">
    ... 
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Dec 2005
    Location
    São Paulo, Brasil
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?
    Tks
    William

  • #4
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You cannot change the type of an HTML input after it has been added to the DOM. The property becomes "Read Only".
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by SpirtOfGrandeur
    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:
    PHP Code:
    <!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
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    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
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #7
    Regular Coder
    Join Date
    May 2005
    Location
    Michigan, USA
    Posts
    566
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Kor
    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.

    Code:
    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.
    Note: I do not test code. I just write it off the top of my head. There might be bugs in it! But if any thing I gave you the overall theory of what you need to accomplish. Also there are plenty of other ways to accomplish this same thing. I just gave one example of it. Other ways might be faster and more efficient.

  • #8
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by SpirtOfGrandeur
    The trick here is to use the DOM to create a new element and remove the old element.
    That was my first thought too...
    Quote Originally Posted by Kor
    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...
    Last edited by Kor; 01-24-2006 at 09:22 PM.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •