Jump to content

[SOLVED] can anybody explain 5 short lines of JS code to me??


cluce

Recommended Posts

need help on what this lines are doing?

 

// create a link with an onClick event which will

// control the sorting of the table

var linkEl = createElement('a');

linkEl.href = '#';

linkEl.onclick = this.headingClicked;

linkEl.setAttribute('columnId', i);

linkEl.title = 'Click to sort';

 

Link to comment
Share on other sites

need help on what this lines are doing?

 

// create a link with an onClick event which will

// control the sorting of the table

var linkEl = createElement('a');

linkEl.href = '#';

linkEl.onclick = this.headingClicked;

linkEl.setAttribute('columnId', i);

linkEl.title = 'Click to sort';

 

 

Step by step:

var linkEl = createElement('a');

 

The variable linkEl now contains an HTML anchor/link element.

 

linkEl.href = '#';

 

The href attribute of the link is set to '#.'

 

linkEl.onclick = this.headingClicked;

 

This assigns linkEl's onclick event handler the function headingClicked();  Whenever the link that linkEl holds is clicked, that function will execute (providing it was coded correctly).

 

linkEl.setAttribute('columnId', i);

 

This creates the attribute 'columnId' for the link, and sets the value to whatever is currently in the 'i' variable.  I can only assume that this code is executed within a for-loop whose job is to create a table with these links.

 

linkEl.title = 'Click to sort';

 

The last line assigns the text 'Click to sort' to the link's title attribute.  If the link is moused over, this text will appear near the mouse pointer.

 

Hope this helps! :)

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.