Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7445107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:44:34+00:00 2026-05-29T11:44:34+00:00

I’ve a silly problem. Depending on a the visibility of a div I want

  • 0

I’ve a silly problem.

Depending on a the visibility of a div I want to set a hidden value

if($('#crewMember').is(':visible')) {
  $('#visibility').attr('value', 'hidden')
} else {
  $('#visibility').attr('value', 'visible')
}

This works. I’ve checked it via FireBug and I can see that the HTML has changed.

But when I try to get this value after form submission I get the original value, not the changed value.

echo $_POST['visibility']
//returns default value, not the adjusted valueHow come?

How come?

EDIT
SOme example code

<html>
    <script type="text/javascript">
       $(document).ready(function() {

    $('#div').click(function() {
      $('#visibility').val('hidden');
      $('#value').html('hidden value: ' + $('#visibility').val());
    });

    $('#value').html('hidden value: ' + $('#visibility').val());
  });

  <body>
        <form method="post">
            <div id="div">
                click this area to change value
            </div>

            <div id="value"> <!-- This div will show the actual value of the hidden field -->
            </div>
            <input type="hidden" id="visibility" name="visibility" value="initial value" />
            <input type="submit" name="button" value="button" />
        </form>
    </body>
</html>

When I submit this form the $_POST[‘visibility’] always contains the string ‘initial value’.
Even when I changed the value with JS to ‘hidden’;

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:44:34+00:00Added an answer on May 29, 2026 at 11:44 am

    As ManseUK said, Change $("").attr('value',...) to $("").val(...) (see the jQuery documentation for .val)

    But to answer the question as to why, you need to understand how DOM objects work in JavaScript. For every element (div, p, a, img, form, input, whatever…) every single element is an object in the DOM.

    Here’s the important part: the element has a value, but also each attribute has a value. In jQuery, when you use .attr('value',...), what you are really doing is creating an attribute with the name of ‘value’. But this does NOT change the element value, which stays the same.

    In firebug, it will read the JavaScript attributes and layer it on top of the element, which explains why it showed up that way in firebug. However, when it comes time to submit the data to the server, the browser will NOT submit the attribute values – only the element’s value (which has not changed). But using .val() will change the element value, which is what will be submitted to the server.

    Perhaps this will make more sense in raw JavaScript, instead of jQuery:

    the equivalent of .attr() is: element.setAttribute("value","...");

    the equivalent of .val() is: element.value = "...";

    Do you see the difference? In .attr() you never changed the value, only the attribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.