Home > Enterprise >  how to set previousvalue attribute in input tag using jquery
how to set previousvalue attribute in input tag using jquery

Time:12-10

is there any solution to set value for previousvalue attribute for input field using javascript or jquery in the browser console?

I am trying to set the value to input field it's working but at the same time, I need to set the value to previousvalue attribute which is in the tag.

     <input type="text" value="" id="test" previousvalue=""/>  

to set value i am using document.getElementById("test").value="add";

CodePudding user response:

If you mean you want value and previousvalue have the same value you can do it like this (imagine input has inp id):

const input = $('#inp')
input.attr('previousvalue', input.attr('value'))

generaly you can set attribute of tag in JQ like below structure:

$(el).attr('attr-name', 'attr-value');\

in pure JS you can do it like this:

el.setAttribute('attr-name', 'attr-value');

CodePudding user response:

<input id="input" type="text" value="" previousvalue="" />
document.getElementById('input').addEventListener('change', function() {
    this.previousvalue = this.value;
    const { value, previousvalue } = this;
    console.log(value, previousvalue);
});

  • Related