0

I have a select box which is created with PHP like:

echo " <option value=\"".$row['CatId']."\">".$row['CatNaam']."</option>";

Where catid is the id which needs to be sent.

Under the select box I have a jQuery clickable div like:

 <div id="addtag" style="cursor: pointer;" onclick="clicktag();">

In my script I have a function clicktag like:

function clicktag()
{
$('#addtag').hide();
alert("Tag added ");
}
</script>

What I want to do is alert the selected value(the catid) so the function must do like:

function clicktag()
{
$('#addtag').hide();
alert("Tag added "+tagid);
}
</script>

But how can I get tagid? I tried a lot of examples but can't get it working.

6 Answers 6

2

You need a reference to the <select> element in order to get its value. How you get that kind of depends on how the HTML looks, but the easiest way is to give it an ID (if it doesn't have one already) and select it using that:

<select name="yourSelect" id="yourSelect">
    // option tags here
</select>

Then the code:

function clicktag() {
    var tagId = document.getElementById('yourSelect').value;
    // the rest of your code
}
Sign up to request clarification or add additional context in comments.

Comments

1

try val()

function clicktag()
{
 $('#addtag').hide();
 alert("Tag added "+ $('#yourSelectID').val());
}

Comments

0

Use the change event for the select box:

//where #selectBox is id of your select box
$('#selectBox').change(function() {
    var currentValue = $('#selectBox :selected').val();
    alert(selectVal);
});

Comments

0

Use .val() to get the <select> element's current value.

Example:

<select name="mySelectElement" id="selectElement">
    <option value="0">First element</option>
    <option value="1">Second element</option>
    ...
</select>

<script type="text/javascript">
function onClick() {
    alert('The selected value is ' + $('#selectElement').val());
}
</script>

Comments

0
 $('#selectBox').live('change',function(){
   var currentValue = $('#selectBox :selected').val();
   alert(selectVal);
});

1 Comment

.live() has been deprecated for something like a year (if not longer) and was removed entirely in the version of jQuery released a few months ago. There's also absolutely no reason to use a delegated event handler based on the information in the question.
0

The selected value is the catid you say Put it in a var and it will works. you can try to put this in your code

function clicktag()
{
$('#addtag').hide();
 var currentValue = $('#selectBox :selected').val();
   alert(selectVal);

}
</script>

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.