<script>
$(document).ready(function () {
$("#pid1").click(function () {
$(this).hide(SpeedEnteredByUser);
});
});
</script>
<body>
<p id = "pid1">This is paragraph</p>
</body>
I want to call jquery hide function when paragraph is clicked. But I want to pass the speed(i.e. slow/fast) which is entered by the user. How it can be achieved ? Here SpeedEnteredByUser is the value entered by the user(using some form) and I want to pass this value to hide function.
3 Answers 3
Use .val() jQuery method to get the value of a text input (which should hold your speed value!)
$(this).hide(parseInt($("#hide_speed").val(),10));
parseInt() is used to make sure the value of the input is indeed a number. The function accepts 2 arguments, the value itself, and a radix value to validate against (10 means decimal numbers, 2 means binary, 16 means hexa etc).
It's also possible to do with <select>s and other types of inputs.
Comments
fix your ID selector and use value of form element through val() function
<script>
$(document).ready(function () {
$("#pid1").click(function () {
$(this).hide(parseInt($("#youtextinput").val(),10));
});
});
</script>
<body>
<p id = "pid1">This is paragraph</p>
<input type="text" id="youtextinput"/>
</body>
Comments
Your question is quite vague ... but here goes ... to enable the user to select a speed add this to your HTML :
<select id="UserSelectedSpeed">
<option value="slow">Slow</option>
<option value="fast">Fast</option>
</select>
then do the following :
$(document).ready(function () {
$("#pid1").click(function () {
$(this).hide($('#UserSelectedSpeed').val());
});
});
$('#UserSelectedSpeed').val() gets the currently selected value from the select list.
slow / fastetc ?