0

I'm making a form where the users fill in a title which they can size up or down. I have a preview window that changes when they click on the "size up button". But I want to store this in a hidden form to get the value when posting.

HTML - FORM

<input id="title" name="title" />
<input id="titlesize" name="titlesize" value="50" />
<div id="sizeUp">Size up!</div>

HTML - PREVIEW WINDOW

<h2 id="titlepreview" style="font-size: 50px;">Title</h2>

Javascript

$(document).ready(function() {
$("#sizeUp").click(function() {
        $("#titlepreview").css("font-size","+=5"),
        $("#titlesize").val("+=5");   // <-- Here's the problem
});

Any ideas?

4 Answers 4

3

Try this using the .val( function(index, value) ):

$(document).ready(function () {
    $("#sizeUp").click(function () {
        $("#titlepreview").css("font-size", "+=5"),
        $("#titlesize").val(function (index, value) {
            return parseInt(value, 10) + 5;
        });
    });
});

FIDDLE DEMO

Sign up to request clarification or add additional context in comments.

1 Comment

@HundkartanJonas: Here's a very good resource for parseInt. Have a look :)
0

You need parseInt to handle strings as numbers.

$("#sizeUp").click(function () {   
 var obj = $("#titlesize");
 var value = parseInt(obj.val());
 obj.val(value + 5);
});

Comments

0

OK, I'm not entirely sure where the problem is here, but here's a way of going about it anyway:

If you want a range of sizes so you can't get a title too big or small, you could (while this is long-winded) make a css class for each size.

Then, you could use JqueryUI's .addClass() and .removeClass. With these you could do something like:
$("#sizeupbutton").click(function(e){
$(#title).removeClass("size45");
$(#title).addClass("size50");
});

Sorry if I've completely got your question wrong, but good luck!

Edit: OK, now i think i understand what you want, I would advise you check out Vucko's answer below.

Comments

0

you can get variable like this:

$(document).ready(function () {
$("#sizeUp").click(function () {
    $("#titlepreview").css("font-size", "+=5");
    var up=parseInt(($("#titlepreview").css("font-size")),10);
    $("#titlesize").val(up);
 });
});

example:fiddle

Comments

Your Answer

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.