Foros del Web » Programando para Internet » Javascript »

pasar el valor de un div a un imput

Estas en el tema de pasar el valor de un div a un imput en el foro de Javascript en Foros del Web. hola muy buenas tengo un editor de texto que por lo visto funciona de fabula pero necesito pasar el valor que se crea en un ...
  #1 (permalink)  
Antiguo 29/02/2012, 11:27
 
Fecha de Ingreso: agosto-2011
Ubicación: barcelona
Mensajes: 237
Antigüedad: 12 años, 8 meses
Puntos: 1
pasar el valor de un div a un imput

hola muy buenas

tengo un editor de texto que por lo visto funciona de fabula pero necesito pasar el valor que se crea en un div en este caso

<div id="txt"></div>

que esta dentro de una etiqueta iframe que teóricamente recoge el valor de lo que se escribe en el editor y pasarlo a este imput del formulario

<input type="text" name="titulo" id="titulo" />

pero vamos que no hay manera, la base de datos me manda que no se han introducido los datos y es por que no se pasa el valor de div al imput pues he probado directamente escribiendo en el imput y la base de datos si recoge los datos supongo que el error viene dado por alguna de las funciones de javascript supongo que la function
setHidden()

que esta arriba de todo que es la encargada de pasar el valor pero no se donde esta el error alguien ve por donde esta el fallo.

agradecería alguna pista este es el código, supongo que puede servir si se arregla el fallo para mucha gente gracias.

Código HTML:
Ver original
  1. <script type="text/javascript">
  2. function setHidden()
  3. {
  4. document.getElementById('titulo').value =
  5. textEditor.document.getElementById('txt').document.body.innerHTML;
  6. }
  7. </head>
  8. <body onLoad="def()"><center>
  9. <div style="width:500px; text-align:left; margin-bottom:10px ">
  10.  
  11.  
  12. <input type="button" id="bold" style="height:21px; width:21px; font-weight:bold;" value="B" onClick="fontEdit('bold')" />
  13. <input type="button" id="italic" style="height:21px; width:21px; font-style:italic;" value="I" onClick="fontEdit('italic')" />
  14. <input type="button" id="underline" style="height:21px; width:21px; text-decoration:underline;" value="U" onClick="fontEdit('underline')" /> |
  15. <input type="button" style="height:21px; width:21px;"value="L" onClick="fontEdit('justifyleft')" title="align left" />
  16. <input type="button" style="height:21px; width:21px;"value="C" onClick="fontEdit('justifycenter')" title="center" />
  17. <input type="button" style="height:21px; width:21px;"value="R" onClick="fontEdit('justifyright')" title="align right" /> |
  18. <select id="fonts" onChange="fontEdit('fontname',this[this.selectedIndex].value)">
  19.    <option value="Arial">Arial</option>
  20.    <option value="Comic Sans MS">Comic Sans MS</option>
  21.    <option value="Courier New">Courier New</option>
  22.    <option value="Monotype Corsiva">Monotype</option>
  23.    <option value="Tahoma">Tahoma</option>
  24.    <option value="Times">Times</option>
  25. <select id="size" onChange="fontEdit('fontsize',this[this.selectedIndex].value)">
  26.    <option value="1">1</option>
  27.    <option value="2">2</option>
  28.    <option value="3">3</option>
  29.    <option value="4">4</option>
  30.    <option value="5">5</option>
  31. <select id="color" onChange="fontEdit('ForeColor',this[this.selectedIndex].value)">
  32.    <option value="black">-</option>
  33.    <option style="color:red;" value="red">-</option>
  34.    <option style="color:blue;" value="blue">-</option>
  35.    <option style="color:green;" value="green">-</option>
  36.    <option style="color:pink;" value="pink">-</option>
  37. </select> |
  38. <input type="button" style="height:21px; width:21px;"value="1" onClick="fontEdit('insertorderedlist')" title="Numbered List" />
  39. <input type="button" style="height:21px; width:21px;"value="●" onClick="fontEdit('insertunorderedlist')" title="Bullets List" />
  40. <input type="button" style="height:21px; width:21px;"value="←" onClick="fontEdit('outdent')" title="Outdent" />
  41. <input type="button" style="height:21px; width:21px;"value="→" onClick="fontEdit('indent')" title="Indent" />
  42. </div>
  43. <iframe id="textEditor" style="width:500px; height:170px;">
  44. <div id="txt"></div>
  45. </center>
  46. <script type="text/javascript">
  47. <!--
  48. textEditor.document.designMode="on";
  49. textEditor.document.open();
  50. textEditor.document.write('<head><style type="text/css">body{ font-family:arial; font-size:13px; }</style> </head>');
  51.  
  52. textEditor.document.close();
  53. function def()
  54. {
  55.   document.getElementById("fonts").selectedIndex=0;
  56.   document.getElementById("size").selectedIndex=1;
  57.   document.getElementById("color").selectedIndex=0;
  58. }
  59. function fontEdit(x,y)
  60. {
  61.   textEditor.document.execCommand(x,"",y);
  62.  
  63.   textEditor.focus();
  64. }
  65. -->
  66. <form action="acceso3.php" method="post" onSubmit="setHidden()">
  67. <input type="text" name="titulo" id="titulo" />
  68. <input type="text" name="pagina" id="pagina" />
  69. <input type="submit" value="submit" />
  70. </form>
  71.  
  72. </body>
  73. </html>
  #2 (permalink)  
Antiguo 29/02/2012, 12:10
Colaborador
 
Fecha de Ingreso: mayo-2008
Ubicación: $MX['VZ']['Xalapa']
Mensajes: 3.005
Antigüedad: 15 años, 11 meses
Puntos: 528
Respuesta: pasar el valor de un div a un imput

El problema es de javascript, no de php.

En principio, quizá me equivoque, es que tu metes dentro del iframe un div, sin embargo, al hacer el iframe editable y escribirle dentro con textEditor.document.write, estás borrando dicho div. No sé de donde sacaste el ejemplo, pero no creo que debas indicar el div dentro del iframe y tampoco que para pasar su valor al input debas copiar el innerhtml del div, ya que para cuando lo haces ya no existe por que se ha sobreescrito.

Tal vez en vez de
textEditor.document.getElementById('txt').document .body.innerHTML;

debería ser

textEditor.document.body.innerHTML;

Aunque no estoy seguro ya que no estoy probando el código.
  #3 (permalink)  
Antiguo 29/02/2012, 13:06
 
Fecha de Ingreso: agosto-2011
Ubicación: barcelona
Mensajes: 237
Antigüedad: 12 años, 8 meses
Puntos: 1
Respuesta: pasar el valor de un div a un imput

ocp001a ya esta solucionado pero no se si funciona en todos los navegadores en google crome si aqui el codigo completo funcionando, gracias ahora abria que mejorar las funciones que pueda realizar mas cositas.




Código HTML:
Ver original
  1. <script language="javascript">
  2.  
  3. function setHidden()
  4. {
  5. var frameContent=textEditor.document.body.innerHTML;
  6. document.getElementById('titulo').value=frameContent;
  7. }
  8.  
  9. </head>
  10. <body onLoad="def()"><center>
  11. <div style="width:500px; text-align:left; margin-bottom:10px ">
  12. <input type="button" id="bold" style="height:21px; width:21px; font-weight:bold;" value="B" onClick="fontEdit('bold')" />
  13. <input type="button" id="italic" style="height:21px; width:21px; font-style:italic;" value="I" onClick="fontEdit('italic')" />
  14. <input type="button" id="underline" style="height:21px; width:21px; text-decoration:underline;" value="U" onClick="fontEdit('underline')" /> |
  15. <input type="button" style="height:21px; width:21px;"value="L" onClick="fontEdit('justifyleft')" title="align left" />
  16. <input type="button" style="height:21px; width:21px;"value="C" onClick="fontEdit('justifycenter')" title="center" />
  17. <input type="button" style="height:21px; width:21px;"value="R" onClick="fontEdit('justifyright')" title="align right" /> |
  18. <select id="fonts" onChange="fontEdit('fontname',this[this.selectedIndex].value)">
  19. <option value="Arial">Arial</option>
  20. <option value="Comic Sans MS">Comic Sans MS</option>
  21. <option value="Courier New">Courier New</option>
  22. <option value="Monotype Corsiva">Monotype</option>
  23. <option value="Tahoma">Tahoma</option>
  24. <option value="Times">Times</option>
  25. <select id="size" onChange="fontEdit('fontsize',this[this.selectedIndex].value)">
  26. <option value="1">1</option>
  27. <option value="2">2</option>
  28. <option value="3">3</option>
  29. <option value="4">4</option>
  30. <option value="5">5</option>
  31. <select id="color" onChange="fontEdit('ForeColor',this[this.selectedIndex].value)">
  32. <option value="black">-</option>
  33. <option style="color:red;" value="red">-</option>
  34. <option style="color:blue;" value="blue">-</option>
  35. <option style="color:green;" value="green">-</option>
  36. <option style="color:pink;" value="pink">-</option>
  37. </select> |
  38. <input type="button" style="height:21px; width:21px;"value="1" onClick="fontEdit('insertorderedlist')" title="Numbered List" />
  39. <input type="button" style="height:21px; width:21px;"value="â—" onClick="fontEdit('insertunorderedlist')" title="Bullets List" />
  40. <input type="button" style="height:21px; width:21px;"value="â†" onClick="fontEdit('outdent')" title="Outdent" />
  41. <input type="button" style="height:21px; width:21px;"value="→" onClick="fontEdit('indent')" title="Indent" />
  42. </div>
  43. <form action="acceso3.php" method="post" onsubmit="setHidden()">
  44.   <input type="hidden" name="titulo" id="titulo" />
  45. <input type="submit" value="submit" />
  46. </form>
  47. </center>
  48. <script type="text/javascript">
  49. <!--
  50. function def()
  51. {
  52. var testframe = document.createElement("iframe");
  53. testframe.name = testframe.id = "textEditor";
  54.  
  55.  
  56. if (testframe.addEventListener){
  57. testframe.addEventListener("load",function(e){this.contentWindow.document.designMode = "on";}, false);
  58. } else if (testframe.attachEvent){
  59. testframe.attachEvent("load", function(e){this.contentWindow.document.designMode = "on";});
  60. }
  61.  
  62. document.body.appendChild(testframe);
  63.  
  64. textEditor.document.designMode="on";
  65. textEditor.document.open();
  66. textEditor.document.write('<head><style type="text/css">body{ font-family:arial; font-size:13px; }</style> </head>');
  67. textEditor.document.close();
  68. textEditor.focus();
  69.  
  70.  
  71.  
  72. }
  73. function fontEdit(x,y)
  74. {
  75. textEditor.document.execCommand(x,"",y);
  76. textEditor.focus();
  77. }
  78. -->
  79. </body>
  80. </html>

gracias

Etiquetas: formulario, html, imput
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 19:34.