Click here to Skip to main content
15,886,963 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Progress Bar Pin
Manish K. Agarwal2-Sep-12 19:22
Manish K. Agarwal2-Sep-12 19:22 
AnswerRe: Progress Bar Pin
twseitex2-Sep-12 8:24
twseitex2-Sep-12 8:24 
GeneralRe: Progress Bar Pin
Manish K. Agarwal2-Sep-12 19:51
Manish K. Agarwal2-Sep-12 19:51 
GeneralRe: Progress Bar Pin
twseitex4-Sep-12 3:18
twseitex4-Sep-12 3:18 
Questionget the checkbox index or table row index with javascript Pin
osman makhtoom30-Aug-12 13:01
professionalosman makhtoom30-Aug-12 13:01 
AnswerRe: get the checkbox index or table row index with javascript Pin
enhzflep30-Aug-12 13:26
enhzflep30-Aug-12 13:26 
GeneralRe: get the checkbox index or table row index with javascript Pin
osman makhtoom30-Aug-12 13:45
professionalosman makhtoom30-Aug-12 13:45 
GeneralRe: get the checkbox index or table row index with javascript Pin
enhzflep30-Aug-12 14:36
enhzflep30-Aug-12 14:36 
You're welcome. Smile | :)

Well, you can't do it with PHP. That is to say, PHP is only good for performing calculations before outputting to a web-browser.

You can't do it with the keyboard, since the keyboard just allows you to create the page that is then displayed. Well, for the purpose of this discussion, PHP works just like the keyboard, except a little faster.

So, where does that leave you if you want to create a page with PHP and have it behave in the same way?

Well, you simply put the appropriate javascript into a js file. You then link that file to the html output, with a
<script src='......'></script> tag.

You can see from the last line of the code I posted, that the onload function for the document calls a function which attaches an event handler to each of the table-rows in the page.

So, you could put the function into the javascript file.
You can link that file to the output of the PHP
You can call the function either with <body onload='yourInitFunc();'> in the html
or with window.onload = yourInitFunc; in the javascript.

Presumably, you're assembling the page in PHP perl-style, I.e you're using print, printf or echo to output each line of content as you go. In that case, you just output the required tags with a print statement at the appropriate place.

If on the other hand, you're using DOM objects to construct the page - then you'll need to create the script node & set it's src attribute to point to the javascript file.


To be perfectly honest, if I was in your shoes then I'd just make a quick html file that did nothing but try to attach a function to the onclick event of each of the table rows.
Okay, I got bored - have a play around with this. Make it work the way you'd like, then output the file dynamically, instead of with the keyboard (or in your case, copy/paste)


JavaScript
<!doctype html>
<html>
<head>
<script>

function myRowClickFunction()
{
	alert('row clicked');
}

function myBtnClicked()
{
	alert('btn clicked');
}

function addBtnToRow()
{
	// *** 'this' points to the row that was clicked ***
	
	// make a new table-cell element
	td = document.createElement('td');
	
	// make a new input element
	btnInput = document.createElement('input');
	btnInput.setAttribute('value', 'click me');
	btnInput.setAttribute('type', 'button');
	btnInput.onclick = myBtnClicked;
	
	// place the btn inside the new table cell
	td.appendChild(btnInput);

	// place this new cell inside the row that was clicked
	this.appendChild(td);
	
	// remove the onclick handler from the row, to prevent adding a button multiple times
	this.onclick = null;
}

// attach the specified function to the onclick event of all 'tr' elements in the document
function attachEventHandlerToTableRows(functionToAttach)
{
	var i, num;
	var tableRows = document.getElementsByTagName('tr');

	num = tableRows.length;
	for (i=0; i<num; i++)
	{
		tableRows[i].onclick = functionToAttach;
	}
}


// Sample onInit function.
// make sure you only use one function or the other here
function myInit()
{
	// the row will just tell us that we've clicked it
	attachEventHandlerToTableRows(myRowClickFunction);
	
	// the row will add a cell that contains a button, whenever the row is clicked
	// try commenting out the above line of code and uncommenting this one instead
	//attachEventHandlerToTableRows(addBtnToRow);
}
</script>
</head>
<body onload='myInit();'>
	<table>
		<tbody>
			<tr><td>This is row 1</td></tr>
			<tr><td>This is row 2</td></tr>
			<tr><td>This is row 3</td></tr>
			<tr><td>This is row 4</td></tr>
			<tr><td>This is row 5</td></tr>
		</tbody>
	</table>
</body>
</html>

Make it work. Then do it better - Andrei Straut

GeneralRe: get the checkbox index or table row index with javascript Pin
osman makhtoom30-Aug-12 19:51
professionalosman makhtoom30-Aug-12 19:51 
GeneralRe: get the checkbox index or table row index with javascript Pin
BobJanova30-Aug-12 22:48
BobJanova30-Aug-12 22:48 
GeneralRe: get the checkbox index or table row index with javascript Pin
enhzflep30-Aug-12 23:18
enhzflep30-Aug-12 23:18 
AnswerRe: get the checkbox index or table row index with javascript Pin
enhzflep30-Aug-12 23:24
enhzflep30-Aug-12 23:24 
GeneralRe: get the checkbox index or table row index with javascript Pin
kmoorevs28-Sep-12 6:07
kmoorevs28-Sep-12 6:07 
Questionuse JQuery in C# code Pin
GSingh-Developer29-Aug-12 7:26
GSingh-Developer29-Aug-12 7:26 
AnswerRe: use JQuery in C# code Pin
jkirkerx29-Aug-12 8:01
professionaljkirkerx29-Aug-12 8:01 
GeneralRe: use JQuery in C# code Pin
GSingh-Developer29-Aug-12 8:26
GSingh-Developer29-Aug-12 8:26 
GeneralRe: use JQuery in C# code Pin
jkirkerx29-Aug-12 9:49
professionaljkirkerx29-Aug-12 9:49 
AnswerRe: use JQuery in C# code Pin
BobJanova30-Aug-12 0:18
BobJanova30-Aug-12 0:18 
GeneralRe: use JQuery in C# code Pin
jkirkerx30-Aug-12 7:34
professionaljkirkerx30-Aug-12 7:34 
SuggestionText editor Pin
Nolee K28-Aug-12 2:57
Nolee K28-Aug-12 2:57 
GeneralRe: Text editor Pin
Pete O'Hanlon28-Aug-12 3:06
mvePete O'Hanlon28-Aug-12 3:06 
GeneralRe: Text editor Pin
BobJanova28-Aug-12 3:31
BobJanova28-Aug-12 3:31 
AnswerRe: Text editor Pin
Tadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)28-Aug-12 3:56
protectorTadit Dash (ତଡିତ୍ କୁମାର ଦାଶ)28-Aug-12 3:56 
GeneralRe: Text editor Pin
R. Giskard Reventlov28-Aug-12 9:41
R. Giskard Reventlov28-Aug-12 9:41 
AnswerRe: Text editor Pin
Joan M28-Aug-12 10:00
professionalJoan M28-Aug-12 10:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.