Jump to content

dumb question.


phorcon3

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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

Link to comment
Share on other sites

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;)

Link to comment
Share on other sites

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>

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.