Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace the value of a key in a jquery object
    text
    copied!<p>I have created a modal plugin that has options defined by default or the user. The majority of these values will be passed to a function and updated, when this happens, I want the value to be updated in the options. Ideally I don't want to have to update each individual one, and want to handle and update the object (options) as a whole.</p> <p>the following is the users options:</p> <pre><code>modalDialog({ ID: 'before_unload', title: 'opt.heading', message: 'opt.text', confirm: 'opt.confirm', cancel: 'opt.cancel', link: $(this).attr('href'), }); </code></pre> <p>Each of the options beginning with 'opt.' will have the function performed on them. Here is the code for the modalDialog plugin that deals with the options</p> <pre><code>var options = $.extend(defaults, options); return this.each(function() { var o = options; var reg = '^opt.*'; //regex to find all 'opt.' values console.log(o); $.each(o, function(i, v) { //for each value if(i != 'save'){ if(v.match(reg)){ // match to regex console.log(i+': '+v); var newval = rc.getResource(v); // function to update value console.log(newval); //value has been updated, function works v = i.replace(i, newval); // update the index with new value console.log('New i:v = '+i+': '+v); // checked and it works //Now the question is, how do I update o (options with the new values?) } }; }); </code></pre> <p>In the code above, lets pretend that rc.getResource(v) come back with a different farm animal name depending on which value gets passed to it, I update the value with my new farm animal name, and check that it all matches up and everything is working up until I get to updating the options. I have tried <code>o = options.replace(i,v)</code> and it come back with options.replace is not a function.</p> <p>One thought I did have was after I have created a new value (performed my function) unset/delete the corresponding i:v and add the new value to the object, e.g.</p> <pre><code>i1 = i; v1 = i.replace(i, newval); o -= i:v; o += i1:iv; </code></pre> <p>Alas, my brain is frazzled and am in desperate need of help, which would be greatly appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload