1

I have a jquery selector used to select an object within some plugin code, created by a 3rd party. When I hardcode one search item, it works, but when I attempt to use variables in order to adjust the search, the object is not found.

The 3rd party code does not include id handles on all the CSS code. Thus it is necessary to search for the string, in this case a label and the associated value within the label.

I have searched the internet and tried a variety of samples that seem to work for other, but with no success.

The search that works is this:

var $box = $('label[for="MMDListsRecord_0_title"]');
$box.text('<?php echo $NewLabel ?>');

But when I break the search line up, in order to dynamically choose a row, the search fails. I have tried all these combinations:

var $row = 0;
var $box = 'label[for="MMDListsRecord_'+ $row +'_title"]';
$box.text('<?php echo $NewLabel ?>');
var row = 0;
var $x = 'label[for="MMDListsRecord_'+ row +'_title"]';
var $box = $(x);
$box.text('<?php echo $NewLabel ?>');
var $row = 0;
var $box = $('label[for="MMDListsRecord_'+ $row +'_title"]');
$box.text('<?php echo $NewLabel ?>');
var $row = '0';
var $box = $('label[for="MMDListsRecord_'+ $row +'_title"]');
$box.text('<?php echo $NewLabel ?>');
var $row = "0";
var $box = $('label[for="MMDListsRecord_'+ $row +'_title"]');
$box.text('<?php echo $NewLabel ?>');

Then went to trying to just search for all combination of the title, only to have the jquery search crash or fail.

var $box = $("label[for=~'title");
$box.text('<?php echo $NewLabel ?>');
 var $box = $('label[for=~"_title"]');
$box.text('<?php echo $NewLabel ?>');
var $box = $("[for~='_title'");
$box.text('<?php echo $NewLabel ?>');

All combinations fail to find the code object, except for the whole string

var $box = $('label[for="MMDListsRecord_0_title"]');

What is the secret?

2 Answers 2

2

You're defined $row variable, but used row - it's undefined. https://jsfiddle.net/s4cyqku6/

Sign up to request clarification or add additional context in comments.

5 Comments

Typo, now corrected. Been Trying different combo of row vs $row, given the temperamental nature of JavaScript. . In each code segment, the value is set to 0. Look at code snippet #2. It defines and uses the same variable. Interesting technique though of using jsfiddle.
Second code snippet is invalid, because you just have selector in your $box, not a jquery-object. Snippets #3, #4, #5 and #6 are valid.
Nice to know but none of them work. They do not find the object.
Yes, I tried all those combination within the code..none worked. That is why I was asking the question. Had they worked, the question would have never been submitted.
But you did just give me an idea..let me give that a go. Discussion is always good!
0

Thanks to all. got it working:

$row=0;
var $box = $('label[for="MMDListsRecord_'+ $row +'_title"]');
$box.text('<?php echo $NewLabel ?>');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.