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 6184303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:30:54+00:00 2026-05-24T01:30:54+00:00

I’ve got the following field on an MVC view: @Html.TextBoxFor(model => model.Course.Title, new {

  • 0

I’ve got the following field on an MVC view:

@Html.TextBoxFor(model => model.Course.Title, new { data_helptext = "Old Text" })</span>

In a seperate js file, I want to set the data-helptext attribute to a string value. Here’s my code:

alert($(targetField).data("helptext"));

$(targetField).data("helptext", "Testing 123");

The alert() call works fine, it shows the text “Old Text” in an alert dialog. However, the call to set the data-helptext attribute to “Testing 123” does not work. “Old Text” is still the attribute’s current value.

Am I using the call to data() incorrectly? I’ve looked this up on the web, and I can’t see what I’m doing wrong.

Here’s the HTML markup:

<input data-helptext="Old Text" id="Course_Title" name="Course.Title" type="text" value="" />
  • 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-24T01:30:54+00:00Added an answer on May 24, 2026 at 1:30 am

    It is mentioned in the .data() documentation

    The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery)

    This was also covered on Why don't changes to jQuery $.fn.data() update the corresponding html 5 data-* attributes?

    The demo on my original answer below doesn’t seem to work any more.

    Updated answer

    Again, from the .data() documentation

    The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.

    So for <div data-role="page"></div> the following is true $('div').data('role') === 'page'

    I’m fairly sure that $('div').data('data-role') worked in the past but that doesn’t seem to be the case any more. I’ve created a better showcase which logs to HTML rather than having to open up the Console and added an additional example of the multi-hyphen to camelCase data- attributes conversion.

    Updated demo (2015-07-25)

    Also see jQuery Data vs Attr?

    HTML

    <div id="changeMe" data-key="luke" data-another-key="vader"></div>
    <a href="#" id="changeData"></a>
    <table id="log">
        <tr><th>Setter</th><th>Getter</th><th>Result of calling getter</th><th>Notes</th></tr>
    </table>
    

    JavaScript (jQuery 1.6.2+)

    var $changeMe = $('#changeMe');
    var $log = $('#log');
    
    var logger;
    (logger = function(setter, getter, note) {
        note = note || '';
        eval('$changeMe' + setter);
        var result = eval('$changeMe' + getter);
        $log.append('<tr><td><code>' + setter + '</code></td><td><code>' + getter + '</code></td><td>' + result + '</td><td>' + note + '</td></tr>');
    })('', ".data('key')", "Initial value");
    
    $('#changeData').click(function() {
        // set data-key to new value
        logger(".data('key', 'leia')", ".data('key')", "expect leia on jQuery node object but DOM stays as luke");
        // try and set data-key via .attr and get via some methods
        logger(".attr('data-key', 'yoda')", ".data('key')", "expect leia (still) on jQuery object but DOM now yoda");
        logger("", ".attr('key')", "expect undefined (no attr <code>key</code>)");
        logger("", ".attr('data-key')", "expect yoda in DOM and on jQuery object");
    
        // bonus points
        logger('', ".data('data-key')", "expect undefined (cannot get via this method)");
        logger(".data('anotherKey')", ".data('anotherKey')", "jQuery 1.6+ get multi hyphen <code>data-another-key</code>");
        logger(".data('another-key')", ".data('another-key')", "jQuery < 1.6 get multi hyphen <code>data-another-key</code> (also supported in jQuery 1.6+)");
    
        return false;
    });
    
    $('#changeData').click();
    

    Older demo


    Original answer

    For this HTML:

    <div id="foo" data-helptext="bar"></div>
    <a href="#" id="changeData">change data value</a>
    

    and this JavaScript (with jQuery 1.6.2)

    console.log($('#foo').data('helptext'));
    
    $('#changeData').click(function() {
        $('#foo').data('helptext', 'Testing 123');
    //  $('#foo').attr('data-helptext', 'Testing 123');
        console.log($('#foo').data('data-helptext'));
        return false;
    });
    

    See demo

    Using the Chrome DevTools Console to inspect the DOM, the $('#foo').data('helptext', 'Testing 123'); does not update the value as seen in the Console but $('#foo').attr('data-helptext', 'Testing 123'); does.

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

Sidebar

Related Questions

I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
link Im having trouble converting the html entites into html characters, (&# 8217;) i
i got an object with contents of html markup in it, for example: string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.