Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Javascript Object Node Referencing Help
sonesay
post Feb 19 2008, 07:04 PM
Post #1


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 643
Joined: 20-June 07
From: Auckland
Member No.: 45,102



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

CODE

echo"<form method='post' action='manage.php?manage=news&amp;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.
Go to the top of the page
 
+Quote Post
[John]
post Feb 19 2008, 11:02 PM
Post #2


Newbie [Level 2]
**

Group: Members
Posts: 32
Joined: 19-February 08
Member No.: 58,129



so do you want the button to disable after submission? I'm not sure if i understand the question very well tongue.gif
Go to the top of the page
 
+Quote Post
sonesay
post Feb 19 2008, 11:12 PM
Post #3


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 643
Joined: 20-June 07
From: Auckland
Member No.: 45,102



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.
Go to the top of the page
 
+Quote Post
coolcat50
post Feb 19 2008, 11:28 PM
Post #4


Super Member
*********

Group: [HOSTED]
Posts: 281
Joined: 5-October 07
From: Random Places
Member No.: 51,171
Spam Patrol



I believe there is a way to retrieve objects by name. You could always use that. I don't know exactly how to though.
Go to the top of the page
 
+Quote Post
apacheNewbie
post Feb 20 2008, 05:16 AM
Post #5


Newbie [Level 2]
**

Group: Members
Posts: 30
Joined: 23-November 06
Member No.: 33,877



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>]

CODE
[/codebox]

Because I don't know the sequence number of your form, I will use form id instead. I will use id 'form1' for simplicity.

[codebox]
&lt;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&amp;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

This post has been edited by apacheNewbie: Feb 20 2008, 05:19 AM
Go to the top of the page
 
+Quote Post
sonesay
post Feb 20 2008, 07:23 AM
Post #6


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 643
Joined: 20-June 07
From: Auckland
Member No.: 45,102



I found the solution to this problem
CODE
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
CODE
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.
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Javascript Slideshow Tutorial(3)
  2. Javascript Close Window(12)
  3. One Click Copy And Paste To Clipboard(5)
  4. Adding Rows & Columns In Html Table Using Javascript(1)
  5. I'm Making My Own Javascript Only Rpg :d(7)
  6. What's The Relationship Between Javascript And Java(5)
  7. Javascript : No Right Click Script !@(12)
  8. Hiding <div> Boxes With Javascript(8)
  9. A Simple Javascript Help Required(3)
  10. Help With Javascript Calculator On My Website Please!(1)
  11. My Little Javascript(0)
  12. Highlight A Word In Javascript. Help!(2)
  13. How Do You Make A Javascript Calculator?(11)
  14. Help With Javascript Calculator Returning "nan"(2)
  15. Great Javascript Script Source(2)
  1. Web Applications: J2ee Or Javascript/css/html(1)
  2. Opera Browser + Javascript + Embeded Sound(0)
  3. Javascript - What's Your Browser?(3)
  4. Javascript Events Not Working For Ie(6)
  5. I`m New To Javascript.(5)
  6. Special Wii Javascript(2)
  7. Javascript Error(2)
  8. Is It Possible To Create A Web Based Mmo In Javascript?(4)
  9. Capturing Username Of Computer(3)
  10. Flash And Javascript Interaction(1)
  11. Document.write & Noscript Questions (javascript)(1)
  12. Java Vs Javascript(11)
  13. Adjusting Rows/cols Of Frames In Frameset Using Javascript Is Not Working In Firefox 3 Is Not Working(4)


 



- Lo-Fi Version Time is now: 27th July 2008 - 01:15 AM