Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Need jquery function example

jQuery jquery

  • Please log in to reply
3 replies to this topic

#1 thatsme

thatsme

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 218 posts

Posted 10 September 2012 - 12:21 PM

Here's a html code containing three boxes. When box is clicked, function removeBox is called and according to given id deletes box. That works fine. However, i don't understand how to accomplish the same thing in jquery. I suppose i must somehow make dynamic selector that has altering id value. Could anyone modify my code in jquery style. I would appreciate that.
function removeBox(id){
    document.getElementById(id).parentNode.removeChild(document.getElementById(id));
}

<div>
    <div id="1" onclick="javascript:removeBox('1')"></div>
    <div id="1" onclick="javascript:removeBox('1')"></div>
    <div id="1" onclick="javascript:removeBox('1')"></div>
</div>

  • 0

#2 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 10 September 2012 - 10:54 PM

I think
$(".anything").click(function() {
$(this).remove();
});
+--> in document ready
Where your html looks like:
<div>
	 <div class="anything"></div>
	 <div class="anything"></div>
	 <div class="anything"></div>
</div>

  • 1

#3 thatsme

thatsme

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 218 posts

Posted 11 September 2012 - 01:51 AM

Let's say in function i need id of clicked div. how you get it? Do you again use "this"?
  • 0

#4 wim DC

wim DC

    Roar

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 2681 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Python

Posted 11 September 2012 - 02:01 AM

Yup
$(this).attr('id');

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download