mkkrolik
mkkrolik

Reputation: 1259

How to change own defined attributes

I'm wondering how to change own defined attributes in html tags using JavaScript/jQuery. For example: I have defined attributed called ascending:

<th ascending="true" id="1">some text</th>

and in JavaScript/jQuery I want to change that attribute on "false" I was trying that way, but it doesn't work(I guess this option is only for predefined attributes):

var tag = document.getElementById("1");
    tag.ascending = "false";

Upvotes: 0

Views: 65

Answers (3)

arclite
arclite

Reputation: 528

Try this in jquery

$(function(){
  $('#1').attr('ascending',false);
});

but you should use .prop()

$(function(){
  $('#1').prop('ascending',false);
})

Or in javascript

function changeValue() {
  var tag = getElemendById('1');
  tag.setAttribute('ascending', false);
}

and html for javascript version should look like this:

<th onLoad="changeValue()" data-ascending="true" id="1">some text</th>

I didn't test it but it should work ;)

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Use custom data-* attributes when adding customization, else it wont pass validation! In your case:

<th data-ascending="true" id="1">some text</th>

And to get/set (pure JS):

var tag = document.getElementById("1");
tag.getAttribute("data-ascending"); //get
tag.setAttribute("data-ascending", true); //set

jQuery:

$("#1").data("ascending"); //get
$("#1").data("ascending", true); //set

Upvotes: 2

Neozaru
Neozaru

Reputation: 1130

You can use the "setAttribute" method.

Like this : tag.setAttribute("Ascending","false");

Upvotes: 1

Related Questions