phorcon3 Posted August 10, 2008 Share Posted August 10, 2008 <div id="test" onclick="dothis('value1','value2');">Text</div> is there anyway i can use javascript to do the onclick function for this element without having to click on it? (no cmts bout this bein stupid lol) ..and the onclick has to remain there ..because clicking on it should be an option too.. have something like: document.getElementById('test').dosomethinghere; any ideas? Quote Link to comment Share on other sites More sharing options...
xtopolis Posted August 10, 2008 Share Posted August 10, 2008 you can use onFocus(), so if they tab to it it should execute.. Otherwise, you could do a onload event in the <body> Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 10, 2008 Share Posted August 10, 2008 You need to be a little more specific. How are you wanting the function to be initialized (other than the onclick). As xtopolis stated, you can do it through onfocus of the element or through onload of the page, or there are many other triggers you could use. Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted August 10, 2008 Author Share Posted August 10, 2008 <div id="test_1" onclick="dothis('value1');">Text1</div> <div id="test_2" onclick="dothis('value2');">Text2</div> <div id="test_3" onclick="dothis('value3');">Text3</div> so, lets say i have a list like this ...and i used javascript to be able to use the arrow keys on ur keyboard to select either test_1, test_2 or test_3... so when i use the arrow key to go from test_1 to test_2... and then i hit enter ...how do get the value from test_2 ..which in this case would be value2 ..cause i need that value to insert it into a text field... i hope that makes sense? so i have something like if(event.keyCode == 13){/*do the hit enter function->fetch value*/} it would be easy if i had the value i needed within the div tags ...so i could use just document.getElementById('test_2').innerHTML; ..but thats unfortunately not the case... i hope this makes sense lol Quote Link to comment Share on other sites More sharing options...
xtopolis Posted August 10, 2008 Share Posted August 10, 2008 I'm having trouble following what you're trying to do, do you have a demo up so I can see it? We might need to see what function dothis() is as well..? Quote Link to comment Share on other sites More sharing options...
phorcon3 Posted August 10, 2008 Author Share Posted August 10, 2008 nah, unfortuantely i dont have it online ...the dothis function just takes the value and inserts it into a text field ..thats all ..but anyway, thanks for ur help. i appreciate it. but i guess ill just go with hidden fields.. it works ..buuuut ..well, it works, i guess thats all that matters for now lol thanks again;) Quote Link to comment Share on other sites More sharing options...
xtopolis Posted August 10, 2008 Share Posted August 10, 2008 Hmm, ya sorry, from what I can tell, I still think that onFocus would be what you could use instead of onclick.. Let us know if you get it posted so we can see what you mean. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 11, 2008 Share Posted August 11, 2008 Yeah, I'm still a little confused as to what you are really trying to accomplish. There is no onfocus event for a div. So, you couldn't do a "return" capture to see what field had focus. I would suggest using a form with input fields (you can use CSS t make the input fields look like regular text if you want). Trying to capture a keypress is problematic when being multi-browser compatible. I try to avoid it unless I can't. How about something like this: <html> <head> <style> .noInput { border: 0px; } </style> <script type="text/javascript"> function setVal(fieldObj) { //Reset all fields to white document.getElementById('test_1').style.backgroundColor = '#ffffff'; document.getElementById('test_2').style.backgroundColor = '#ffffff'; document.getElementById('test_3').style.backgroundColor = '#ffffff'; //Set current field to yellow and set value fieldObj.style.backgroundColor = 'yellow'; document.getElementById('theValue').value = fieldObj.value; return true; } </script> </head> <body> <form name="test" onsubmit="return doThis(document.getElementById());"> <input type="text" id="test_1" onfocus="setVal(this);" value="Text1" readonly="readonly" class="noInput" /><br /> <input type="text" id="test_2" onfocus="setVal(this);" value="Text2" readonly="readonly" class="noInput" /><br /> <input type="text" id="test_3" onfocus="setVal(this);" value="Text3" readonly="readonly" class="noInput" /><br /> <br />This would be a hidden field:<br> <input type="text" id="theValue"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
xenophobia Posted August 12, 2008 Share Posted August 12, 2008 Are you trying to do something like Yahoo! autocomplete list? If that is the case, try use Yahoo! free javascript library, YUI. It's contained tons of useful library functions just like prototype, but in more advanced. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.