0

Once a form is submitted, I'd like to get the value of a field called 'featuredOneImage', and apply that the CSS background of a div.

So once the 'Save' button is pressed, here's the jQuery to submit the ajax needed (which saves the values to a db)

$("form#featuredItems").submit(function() {

    // we want to store the values from the form input box, then send via ajax below
    var $form = $(this);        
    $.ajax({
            type: "POST",
            url: "ajax/featuredItems.php",
            data: $form.serialize(),
            success: function(){
                showAction('Featured Items Saved');
                }
        }); // End .ajax function
    return false;
    }); //End submit function()

So, inside the success function, I'd put something like "get value of 'featuredOneImage' and set the background equal to that value for the div with id 'featuredOneBox'"

I just wasn't sure how to tell the jQuery which field to get the value from.

0

2 Answers 2

2

I just edited Shaz's function to the following, I used the jQuery val() function instead:

success: function(){
   showAction('Featured Items Saved');
   $("#featuredOneBox").css("background-image", "url('" + ($('#featuredOneImage').val()) + "')");
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Shaz: Sorry for copying your function.
ëtte That's fine, it's what I get for trying to build an example and not putting more haste into it, haha. ;)
2

Could try:

    success: function(){
          showAction('Featured Items Saved');
          $("#featuredOneBox").css("background-image", "url('" + $('#featuredOneImage').val() + "')");
        }

example

4 Comments

It's not working. I'm wondering if this code should be different if it's getting the value from a <select> field? So, whatever dropdown value is selected, is the name we're using for the background.
Just to make sure, are you using the same code as above? I edited the thing a few times. ;)
You're shorting an ending bracket on the css() function, that's why the code is not working.
Thank you. I used your code and Anriette's above to get the values. Working now! Appreciate the help.

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.