User login

Looping through a set of IDs to set JQuery triggers on that pass a parameter to a function, such that it works

Corrected JavaScript to work for first variable in list (product) as well as second (rental).

Very novice javascript mistake, i used the same variable in the definition of the id selector and then in the function triggered on a change to that id selector (which was on a form element in each case). Of course, when that is triggered, the variable is not the same as it was in the first rotation, but rather whatever it was in the last. (This was defined in a for loop.) The solution i used was to get the id from the this object and parse it to get the original variable string back to pass into the action function.

There are probably better ways.

The problem is easy to see:

alert($(this).attr("id"));
alert(producttype);

Gives edit-attributes-1-purchase
and rental
respectively when clicking on a purchase link-- because rental is what the producttype variable is after all the looping is done. So even though the trigger is set up correctly for purchase, the code that it triggers, when it triggers it, has the wrong parameter.

The working main part of the function:

$(document).ready(function() {
      var str = $(this).attr("id").substr(18); // Remove 'edit-attributes-1-'.
      AgaricGenartsCommerce.formUpdate(str, true);
    });
    // We always have to check price, but in these cases can skip showhide.
    $('#edit-qty-' + producttype).change(function() {
      var str = $(this).attr("id").substr(9); // Remove 'edit-qty-'.
      AgaricGenartsCommerce.formUpdate(str);
    });
    if (producttype == 'rental') {
      $('#edit-attributes-10').change(function() {
        AgaricGenartsCommerce.formUpdate(producttype);
      });
    }
  }
});

References:

http://www.w3schools.com/jsref/jsref_substring.asp

http://devkick.com/blog/parsing-strings-with-jquery/
http://www.mail-archive.com/jquery-en@googlegroups.com/msg86859.html
http://stackoverflow.com/questions/1109566/ridiculously-simple-jquery-reusable-function-question

jquery print this object
Couldn't see anything useful in this object. Could i assign something to it?

var string = '';
for(var a in this){
  string = string + a + ': ' + this[a] + " \n";
  // a would be the key ie 'title'
  // obj[a] would get the value ie 'mytitle'
}
alert('this: ' + string);

The page at http://genarts.dev says:this: jQuery1267484933320: 1120

addEventListener: function addEventListener() {
[native code]
}
querySelector: function querySelector() {
[native code]
}
querySelectorAll: function querySelectorAll() {
[native code]
}
scrollWidth: 142
clientLeft: 2
clientHeight: 75
clientWidth: 142
clientTop: 1
getClientRects: function getClientRects() {
[native code]
}
getBoundingClientRect: function getBoundingClientRect() {
[native code]
}
getElementsByClassName: function getElementsByClassName() {
[native code]
}
mozMatchesSelector: function mozMatchesSelector() {
[native code]
}
baseURI: http://genarts.dev/product/tinder/autodesk/purchase#product-purchase-form
textContent: -- Choose license --Fixed licenseFloating licenseFixed render licenseFloating render license
compareDocumentPosition: function compareDocumentPosition() {
[native code]
}
getUserData: function getUserData() {
[native code]
}
isSameNode: function isSameNode() {
[native code]
}
lookupNamespaceURI: function lookupNamespaceURI() {
[native code]
}
setUserData: function setUserData() {
[native code]
}
lookupPrefix: function lookupPrefix() {
[native code]
}
isDefaultNamespace: function isDefaultNamespace() {
[native code]
}
isEqualNode: function isEqualNode() {
[native code]
}
dispatchEvent: function dispatchEvent() {
[native code]
}
removeEventListener: function removeEventListener() {
[native code]
}
style: [object CSSStyleDeclaration]
contentEditable: inherit
offsetParent: [object HTMLDivElement]
innerHTML: -- Choose license --Fixed licenseFloating licenseFixed render licenseFloating render license
offsetLeft: 248
offsetTop: 505
offsetHeight: 20
offsetWidth: 145
item: function item() {
[native code]
}
name: attributes[1]
form: [object HTMLFormElement]
value: 1
disabled: false
length: 5
selectedIndex: 2
type: select-one
options: [object HTMLOptionsCollection]
size: 0
add: function add() {
[native code]
}
remove: function remove() {
[native code]
}
className: form-select required
id: edit-attributes-1-purchase
title:
tagName: SELECT
removeAttributeNS: function removeAttributeNS() {
[native code]
}
removeAttribute: function removeAttribute() {
[native code]
}
getAttribute: function getAttribute() {
[native code]
}
getElementsByTagName: function getElementsByTagName() {
[native code]
}
setAttribute: function setAttribute() {
[native code]
}
getElementsByTagNameNS: function getElementsByTagNameNS() {
[native code]
}
hasAttributeNS: function hasAttributeNS() {
[native code]
}
setAttributeNS: function setAttributeNS() {
[native code]
}
hasAttribute: function hasAttribute() {
[native code]
}
getAttributeNS: function getAttributeNS() {
[native code]
}
nextSibling: [object Text]
firstChild: [object HTMLOptionElement]
prefix: null
nodeValue: null
childNodes: [object NodeList]
nodeName: SELECT
namespaceURI: http://www.w3.org/1999/xhtml
previousSibling: [object Text]
nodeType: 1
localName: select
lastChild: [object HTMLOptionElement]
ownerDocument: [object HTMLDocument]
pageNode: [object HTMLDivElement]
attributes: [object NamedNodeMap]
appendChild: function appendChild() {
[native code]
}
cloneNode: function cloneNode() {
[native code]
}
normalize: function normalize() {
[native code]
}
hasChildNodes: function hasChildNodes() {
[native code]
}
insertBefore: function insertBefore() {
[native code]
}
replaceChild: function replaceChild() {
[native code]
}
removeChild: function removeChild() {
[native code]
}
hasAttributes: function hasAttributes() {
[native code]
}
tabIndex: 0
blur: function blur() {
[native code]
}
focus: function focus() {
[native code]
}
isSupported: function isSupported() {
[native code]
}
ELEMENT_NODE: 1
ATTRIBUTE_NODE: 2
TEXT_NODE: 3
CDATA_SECTION_NODE: 4
ENTITY_REFERENCE_NODE: 5
ENTITY_NODE: 6
PROCESSING_INSTRUCTION_NODE: 7
COMMENT_NODE: 8
DOCUMENT_NODE: 9
DOCUMENT_TYPE_NODE: 10
DOCUMENT_FRAGMENT_NODE: 11
NOTATION_NODE: 12
getAttributeNode: function getAttributeNode() {
[native code]
}
setAttributeNode: function setAttributeNode() {
[native code]
}
removeAttributeNode: function removeAttributeNode() {
[native code]
}
getAttributeNodeNS: function getAttributeNodeNS() {
[native code]
}
setAttributeNodeNS: function setAttributeNodeNS() {
[native code]
}
lang:
dir:
multiple: false
namedItem: function namedItem() {
[native code]
}
draggable: false
spellcheck: false
getFeature: function getFeature() {
[native code]
}
DOCUMENT_POSITION_DISCONNECTED: 1
DOCUMENT_POSITION_PRECEDING: 2
DOCUMENT_POSITION_FOLLOWING: 4
DOCUMENT_POSITION_CONTAINS: 8
DOCUMENT_POSITION_CONTAINED_BY: 16
DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC: 32
scrollTop: 0
scrollLeft: 0
scrollHeight: 75
firstElementChild: [object HTMLOptionElement]
lastElementChild: [object HTMLOptionElement]
previousElementSibling: null
nextElementSibling: [object HTMLDivElement]
childElementCount: 5
children: [object HTMLCollection]
classList: form-select required

Searched words: 
jquery two functions jquery reuse function twice in one page jquery two functions different paramater attach run jquery two functions different parameter attach run jquery string functions jquery substring jquery variable function names jquery "variable functions" jquery reuse functions in for loop

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.