Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery: Select the element that called the function

Tags:

jquery

I'm calling a function in the element itself via an onclick attribute because I need php to dynamically give a value in one of the function's parameters. When I try to reference the calling element in the function via $(this), it ends up referencing the entire window and not the element. How do I fix this?

like image 632
Allen Gingrich Avatar asked Oct 30 '10 07:10

Allen Gingrich


People also ask

What is $( function () in jQuery?

So a jQuery function, which is prefixed with the $ or the word jQuery generally is called from within that method. $(document). ready(function() { // Assign all list items on the page to be the color red. // This does not work until AFTER the entire DOM is "ready", hence the $(document).ready() $('li').

What does jQuery selector return?

The jQuery Object: The Wrapped Set: Selectors return a jQuery object known as the "wrapped set," which is an array-like structure that contains all the selected DOM elements. You can iterate over the wrapped set like an array or access individual elements via the indexer ($(sel)[0] for example).

Which jQuery function is used to get the text content of an element?

jQuery text() Method The text() method sets or returns the text content of the selected elements. When this method is used to return content, it returns the text content of all matched elements (HTML markup will be removed).

Which operator in jQuery allows to select an element and perform an action on it?

jQuery selectors allow you to select and manipulate HTML element(s). jQuery selectors are used to "find" (or select) HTML elements based on their name, id, classes, types, attributes, values of attributes and much more.


1 Answers

Try to send your element as a parameter to your function like that :

<input type="text" onclick="myfunction(this);"></input>

your function should be :

<script>
  function myfunction(currentElement){
    // ...
  }
</script>
like image 187
Canavar Avatar answered Sep 25 '22 15:09

Canavar