How can I add a tagname(data-state) to my Hidden input with attr?

Asked

Viewed 90 times

2

I am not able to add a data attribute(data-state) as I did with the other tag(value, type, name, etc) as shown in the following image:

 $(".clicka").on("click", function () {
        $('#calendartable .tdfull').each(function (k) {
            if ($(this).data("id")) {
                $('<input/>').attr({
                    type: 'hidden',
                    name: 'disponibilidade[' + k + ']',
                    value: $(this).data("id"),
                    state: $(this).data("pending"),
                }).appendTo('form');
            }
            $('<input/>').attr({
                type: 'hidden',
                name: 'disponibilidade[' + k + '].titulo',
                value: $(this).data("start") + ' - ' + $(this).data("end"),
                state: $(this).data("pending"),
            }).appendTo('form');
            $('<input/>').attr({
                type: 'hidden',
                name: 'disponibilidade[' + k + '].inicio',
                value: $(this).data("start"),
                state: $(this).data("pending"),
            }).appendTo('form');
            $('<input/>').attr({
                type: 'hidden',
                name: 'disponibilidade[' + k + '].fim',
                value: $(this).data("end"),
                state: $(this).data("pending"),
            }).appendTo('form');
        });

He doesn’t recognize the state, and I think he’s doing something wrong. I wanted to put the state in "pending". Is there any event that does? I searched for deferred but did not get the idea. Thank you.

2 answers

2

To add an attribute you can do it in two different ways.

1º using jQuery.attr()

$('.elemento').attr('data-state', 'valor');

2º using jQuery.data()

$('.elemento').data('state', 'valor');

Documentation jQuery.date()

  • 1

    Look at how I did input. I had already seen this documentation, however I was trying to put in my input. It is possible to do my $(this). data("state", "pending") ?

  • do so, where this state places 'data-state'

  • that way does not, gives error because of the "-". I tried @Sergio’s second hypothesis and it is not working either. HTML shows me this: <input type="Hidden" name="availability[3]. end" value="2015-02-27 15:00"> E must appear this: <input type="Hidden" name="availability[3]. end" value="2015-02-27 15:00" data-state="pending">

1


There is no attribution state in HTML standards as exists value, name and type, then jQuery keeps what he wants but internally.

That is to say, what you’re doing will work, but I suggest that these inputs are created using fields data-, that is to say: data-state.

An example would be:

        $('<input/>').attr({
            type: 'hidden',
            name: 'disponibilidade[' + k + '].fim',
            value: $(this).data("end"),
            'data-state': $(this).data("pending"),
        }).appendTo('form');

or using the jQuery method: .data():

        $('<input/>').attr({
            type: 'hidden',
            name: 'disponibilidade[' + k + '].fim',
            value: $(this).data("end")
        }).data('state', $(this).data("pending")).appendTo('form');

For later reading this value you can use the methods .attr('data-state') or .data('state').

Notice that these fields data- jQuery inserts are not compatible with native Javascript, that is to say that they may not be visible in HTML even though they are saved and can be read. But if you try to fetch via getAttribute won’t work.

To do this in pure Javascript can do with setAttribute.

I leave an example where I mix native JS with jQuery to make the code shorter, but the getter and Setter of the fields date is only in native JS.

Example:

var input = document.createElement('input');
input.setAttribute('data-state', $(this).data("pending"));
$(input).attr({
    type: 'hidden',
    name: 'disponibilidade[' + k + '].fim',
    value: $(this).data("end")
}).appendTo('form');

Example: http://codepen.io/anon/pen/GgZmBz

  • 1

    This is what I wanted. Thanks @Sergio :)

  • I can’t do both forms @Sergio. HTML shows me this: <input type="Hidden" name="availability[3]. end" value="2015-02-27 15:00"> E must appear this: <input type="Hidden" name="availability[3]. end" value="2015-02-27 15:00" data-state="pending">

  • @n1c0t0p one thing is what you see in the console another thing is whether the value was saved or not. Test this in the console after inserting these inputs: $('[name^="disponibilidade"').map(function(){return $(this).data('state');}) what gives?

  • gives empty @Sergio

  • @n1c0t0p take a look here: http://codepen.io/anon/pen/GgZmBz - In HTML the value is not shown but saved.

  • then and how to make it appear? It does not have much sense since value and others appear, because the date-state does not?

  • @n1c0t0p is just like that. Take a look here: http://answall.com/a/42038/129 - jQuery keeps it internally. To do this "visible" you have to use javascript as in this other answer.

  • I really wanted this to be visible, just to be able to understand if you are performing well my "post" which is another step that I have.

  • @n1c0t0p added another idea in the answer to mix JS and jQuery. That would be: http://codepen.io/anon/pen/GgZmBz

  • javascript is also a good hypothesis.

  • @n1c0t0p got to work with the last example?

  • Still no. If you take out the data-state it works and puts everything back the way it was. I know it works. Now, making the three guesses you’ve already given me, it doesn’t work at all...

  • @n1c0t0p quite strange this. Can you put a link or jsFiddle to where you have this code? You see that it works in the examples I gave in the answer, I think there are other problems in your code. See if we can find them...

  • Well here’s just javascript, because this has API’s and things like that, Pis, it’s company design. However, it clicks and sends such inputs, only without the date-state! http://jsfiddle.net/1weycmoo/

  • @n1c0t0p if you join HTML I can therefore work. 2 things that are failing in this fiddle: 1- The jQuery method .on() does not exist yet in version 1.6, the 2- is my mistake should be 'data-state': $(this).data("pending"), that is to say 'data-space'with quotes because object keys cannot have -

  • @n1c0t0p saw the comment above ^ ?

  • Yes I saw, I’ll try, I’m sorry for the delay @Sergio

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.