Honesty Rocks! truth rules.

Javascript Object Node Referencing Help

HOME      >>       Programming


I've got two buttons with an onclick event to set the hidden input field to a certain value then continue submitting the form. I have more than one form on display on the page and I don't want to use ids. Is there a way I can reference it from the button/button onclick event?the forms is laid out like this

echo"<form method='post' action='manage.php?manage=news&id={$rs['id']}'>"; echo"<input type='hidden' name='do' value='' />"; echo"<button type='submit' onclick=\"news_modify('edit');\">Edit</button> "; echo"<button type='submit' onclick=\"news_modify('delete');\">Delete</button>"; echo"</form>";

So basically the idea is to fire off new_modify function and start referencing the hidden input type with name of do and set it to the supplied argument then continue submitting the form.any help is appreciated thank you.


so do you want the button to disable after submission? I'm not sure if i understand the question very well :P


No I'm trying to control the <input type='hidden' name='do' value='' />to be set as either 'edit' or 'delete' depending on what ever button was pushed then submit.


I believe there is a way to retrieve objects by name. You could always use that. I don't know exactly how to though.


If you don't want to use id, you need to know the sequence number of the form in the display.First, you need to access the form object.With id, you can access the form object by using: document.getElementById('$rs['id'])otherwise, you can access it using: document.forms[<form number>]

Because I don't know the sequence number of your form, I will use form id instead. I will use id 'form1' for simplicity.
<script type="text/javascript">function news_modify(type){ if(type=='edit'){ document.getElementById('form1').do.value='edit' } else if(type=='delete'){ document.getElementById('form1').do.value='delete2' } document.getElementById('form1').submit();}<form method='post' action='manage.php?manage=news&id='form1'><input type='hidden' name='do' value='' /><button type='submit' onclick="news_modify('edit');">Edit</button> <button type='submit' onclick="news_modify('delete');">Delete</button></form></script>

Hope it helps


I found the solution to this problem

function news_modify(e, action){ var targ; if (!e) { var e=window.event; } if (e.target) { targ=e.target; } else if (e.srcElement) { targ=e.srcElement; } if (targ.nodeType==3) // defeat Safari bug { targ = targ.parentNode; } var tname; tname=targ.tagName; var targ_do = targ; while(targ_do.name != 'do') { targ_do = targ_do.previousSibling; } targ_do.value = action; //var form = targ.parentNode; //form.action = form.action + '&do='+action; }

and when you call it on <button onclick="news_modify(event, action);" . You must supply the object (I think thats what it is called) event or else it wont work.That function will get the object button and travel up looking for other siblings belonging to the same parent "Form" and get the hidden input with name of 'do'.Although this works a better solution to what I wanted to do didn't required this. I was just not using the form correctly. I could have the same effect and control with

echo"<form method='get' action='manage.php'>"; echo"<input type='hidden' name='manage' value='news' />"; echo"<input type='hidden' name='id' value='{$rs['id']}' />"; //echo"<input type='hidden' name='do' value='' />"; echo"<button name='do' value='edit' type='submit' >Edit</button> "; echo"<button name='do' value='delete' type='submit' >Delete</button>"; echo"</form>";

Its simple yeah I cant believe I didn't know how you could use buttons like that. So in this scenario what ever button is pressed you are able to to submit 'do' with the correct value and then process your page with control.I hope this helps anyone with a similar problem. sone.