Simple HTML/JS event question

Discussion in 'Software' started by Mada_Milty, Apr 2, 2010.

  1. Mada_Milty

    Mada_Milty MajorGeek

    Good Day Geeks,

    I've been writing some javascript functions to use as events in an HTML page for various reasons. Form validation, rollovers, etc.

    In the functions, I've been hard-coding the parent HTMLS object's ID to make it work, but this make my code hard to reuse in the future, as I will have to edit my javascript to use the proper IDs.

    I'm wondering if there is a way for me to pass the HTML object ID as a parameter of the javascript function call.

    Ex.:

    Code:
    <div id="testdiv" onmouseover="js_function([I][B]<div id>[/B][/I])"></div>
    I've seen other developers using the this and the self keywords in either their JS or HTML, and I've tried this route, but I don't think I'm implementing it correctly, as I can't get it to work. If this is CLOSE to right, I'd like an explanation that I can refer to. Does any geek have such a reference?

    If I'm all wrong there, I'm willing to hear any other advice.

    Thanks in advance,

    Milty
     
  2. Mada_Milty

    Mada_Milty MajorGeek

    I've got it.

    The this keyword is a DOM reference to the parent object, or the object that the function is a method of. I had been using the this keyword without pointing to any of it's methods or properties.

    I've since substituted this.id as the parameter for my function call, ex.:

    Code:
    <div id="test_div" onmouseover="js_function([B]this.id[/B])"></div>
    and it is working beautifully.

    Cheers,

    Dave
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds