1

I'm having problem trying to set the value for an input using jquery, basically you click on a link, and it takes you to a different page with a form, and I'm trying to set the value of one of the inputs of that form. I don't know why is not working. Here's my code:

page 1 (where you click the link that performs the function to set the value of the input)

<table class="tablaObjeto">
 <tbody>
 <tr>
 <td>
 <img src="img/ps4Mall.jpg">
 </td>
 </tr>
 <tr>
 <td class="precio">
 <p>$<span id="precioPS4">400</span> - PS4</p>
 </td>
 </tr>
 <tr>
 <td class="botonCompra">
 <a id="botonCompraPS4" href="paginaSolicitudes.html"><img src="img/BotonCompra.png"></a>
 </td>
 </tr>
 </tbody>
</table>

page 2 (form with the input that needs to show the value)

<tr>
 <td class="info">Precio: </td>
 <td class="inputs"><input id="precioI" name="precio" type="text" value=""></td>
</tr>

Here's the javascript (javascript/metodos.js)

$(document).ready(function() {
 $("#botonCompraPS4").click(function() {
 obtienePrecioPS4();
 $("#precioI").val(retornaPrecio());
 });
});
function obtienePrecioPS4() {
 sessionStorage.setItem("precio", $("#precioPS4").text());
}
function retornaPrecio() {
 var r = sessionStorage.getItem("precio");
 return r;
}
greener
5,06913 gold badges56 silver badges98 bronze badges
asked Oct 9, 2014 at 2:08

1 Answer 1

1

try loading the value on document ready, see below

$(document).ready(function() {
 $("#botonCompraPS4").click(function() {
 obtienePrecioPS4();
 });
 // Load value on document ready
 $("#precioI").val(retornaPrecio());
});

Your page1 and page2 should have the same javascript code above

answered Oct 9, 2014 at 2:13
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.