JQuery change id!

I am jQuery noobie and am trying to add a script to change the onClick div id.

Here is a jsfiddle example .

$(function accept() {
    $("div:scrollwrap").attr('scrollwrap','highlight');
});​

Thank:)

+3
source share
3 answers

The first parameter attrshould be the attribute name, not the current value:

$(function accept() {
    $("div#scrollwrap").attr('id','highlight');
});​

However, after reading your jsFiddle code, you have a highlight class, not an identifier. Here is my edited version with what I think you are trying to achieve.

Please note that I have changed the following:

  • Made the .hightlight class more specific by adding an identifier, otherwise the selection style will not override the original.
  • onClick, script, (. JS .click())
  • JS , , , .

( jQuery):

+13

div id scrollwrap, :

 $(document).ready(function() {
   $("#checkbox2").click(function() {
        $("#scrollwrap").attr('id','highlight');
    });
 });

, , . .addClass()/. removeClass()

+2

this is how you change the ID:

$(function accept(){ //makes sure your document is ready
  $('div#scrollwrap').attr('id', 'highlight')
})

(I assume scrollwram is an identifier)

+1
source

Source: https://habr.com/ru/post/1742777/


All Articles