0

I have the following jQuery sequence:

$("button").click(function(){ if ($(this).attr('[id^=facilityName]')) { $("#facilityNameRight").text($(this).text()); } }); 

I need the code in the if statement to only be executed if the button has an id that starts with "facilityName" and I have tried all the combinations for writing the condition. What is wrong?

1
  • Can you add HTML code also. Commented Apr 11, 2016 at 10:45

4 Answers 4

3

Try this : use jquery start with selectors while binding click events instead of checking it in if condition. This will bind click event to button having id start with facilityName and do your required task.

$("button[id^=facilityName]").click(function(){ $("#facilityNameRight").text($(this).text()); }); 
Sign up to request clarification or add additional context in comments.

Comments

2

You should use Attribute Starts With Selector [name^="value"] with button element. Event handlers will be attached to element whose ids start with facilityName

$("button[id^='facilityName']").click(function(){ $("#facilityNameRight").text($(this).text()); }); 

However, still you want the if condition, You should use .is()

Check the current matched set of elements against a selector, element, or jQuery object and return true if at least one of these elements matches the given arguments.

 if ($(this).is('[id^=facilityName]')) { $("#facilityNameRight").text($(this).text()); } 

$("button").click(function() { if ($(this).is('[id^=facilityName]')) { $("#facilityNameRight").text($(this).text()); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button id='facilityNameTest'>facilityNameTest</button> <button id='facilityNameTest2'>facilityNameTest2</button> <button id='Test1'>Test1</button> <div id="facilityNameRight"></div>

1 Comment

Both of the solutions work, but I will stick to the one without the if statement. However, it helped me understand how it should be written so that it works. Thank you!
2

Try:

$("button").click(function(){ if($(this).is('[id^=facilityName]')) { $("#facilityNameRight").text($(this).text()); } });

Comments

0

May be it is possible my answer is duplicate. But I will tell the basic debugging technique that will make you understand what exactly is happening at runtime.

In your chrome(Mostly I used chrome to debug javascript) go to source code tab and on the javascript file see where you are not getting the expected result.

put breakpoint and see what are the things you are getting in watchlist for expected value.

you can just select the complete expression - right click and select add to watchlist.

while the control is on your break point you have the runtime variables alive in that scope.

Now just go to the console and type the variable, expression, or anything which you want to print to see it's value.

for more info check chrome developer tool

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.